0

Jquery get values from dynamically generated input texts

This is an example of what I am trying to achieve. Let me put the html code and describe afterwards

<div class="col-xs-3 ">
                    <label for="pointMap[0]-value">firstkey</label>
                    <input type="text" id="pointMap[0]-value" name="pointsValueMap[firstkey]" value="value1">
                </div>
            
                <div class="col-xs-3 ">
                    <label for="pointMap[1]-value">secondkey</label>
                    <input type="text" id="pointMap[1]-value" name="pointsValueMap[secondkey]" value="value2">
                </div>

I would like to have all the values of however many of these kinds of input type text id (pointMap[i]-value). Please also suggest if there is a better way than having them an array when collected. My mind is blank on what should I be looping against, in a for loop, I am unable to find a terminating condition (size of these input texts).

2
  • Give them a common className and loop through that class Commented Jul 25, 2020 at 13:39
  • I will try it, thanks @charlietfl Commented Jul 25, 2020 at 13:47

1 Answer 1

2

Since you are using jQuery. You can use the attribute contains selector. There is whole list of such selectors in jQuery.

You can store values of such inputs in an array like this

var inputStore = []; //Where you want to store the values

$("input[name*=pointsValueMap]").each(function(){ //Will check for inputs with name containing pointsValueMap
  inputStore.push($(this).val())
});
Sign up to request clarification or add additional context in comments.

2 Comments

What strategy would I use to have an associated array with the key as well? (See the keys that gets printed at top first key and second key? Thanks
Add some data attribute to the input box, <input type="text" data-key="firstkey" .. Then inside the jquery each function you can access it with $(this).data('key') and use it however you like.

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.