0

After creating dynamic inputs along with its dynamic id's by append, only the latest generated input field gives values

Other dynamic input fields return empty string

Note: I'm able to get all values of dynamic inputs by using for loop and pushing inside an array, but i also want to get each input individually on key up . But only final input field created gives value

Here is my fiddle,

https://jsfiddle.net/xd8nvktf/3/

Not able to come up with a solution, Help is appreciated

--Thanks

HTML

<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>

<body>

<button type = "button" class = "btn btn-primary" id = "addInput">Add Input Fields</button>

<div id = "inputAdder">

</div>



</body>

JS/JQUERY

var a = 0;
var fieldValue;

var dataStored = [];
var valueStored = [];

$("#addInput").on("click", function() {

  a += 1;

  fieldValue = `#filter-value${a}`;

  $("#inputAdder").append(

    `<div class = "row" id="appender${a}">
            
<div class="col-md-4 padderSpace">
                <input id="filter-value${a}">
            </div>
            
</div>`
  )
 
   dataStored.push({value:fieldValue});
   console.log(dataStored) 

})


$("#inputAdder").on("keyup",fieldValue,changeFunc);

   
  function changeFunc(){

console.log($(fieldValue).val()); 

valueStored = [];
  
  for(let i = 0;i<=dataStored.length;i++){

valueStored.push({value:$(dataStored[i].value).val()}) 

console.log(valueStored)
  
  }
  
  }
   

1 Answer 1

1

You need to pass current textbox id to your changeFunc function.

function changeFunc(e){
    console.log($("#" + e.target.id).val()); 
  }

Here is working sample.

Sign up to request clarification or add additional context in comments.

4 Comments

I understand but I want only one value of input field being altered. (eg:{value:"aa"} and not [{value:"ss"},{value:"aa"},{value:"bb"}]. I want only the current value of input field being typed
Sorry I didn't catch you by one value of input field being altered
Or lets say I have 4 inputs, can I get the id of the input field I am typing(ex:input 2)? As I have to write a condition based on the id of a single particular field which is currently being typed
Yes I've got it and updated my answer. Pleas check

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.