Eshan I.
Eshan I.

Reputation: 75

Jquery get values from dynamically generated input texts

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).

Upvotes: 0

Views: 78

Answers (1)

AKT
AKT

Reputation: 1061

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())
});

Upvotes: 2

Related Questions