1

So I have a dynamic list of textboxes that have all the same class, like so:

<input type="text" id="value1" class="list-value" />
<input type="text" id="value2" class="list-value" />
<input type="text" id="value3" class="list-value" />
<input type="text" id="value4" class="list-value" />

So when a link is clicked, I need the values from all those textboxes to be loaded into an array:

$('a#link').click(function() {
    //add to array here
});

How can I do that?

1 Answer 1

6

You can do this:

var values = $('.list-value').map(function() { return this.value; }).get();

The ".map()" method will iterate through the list of elements like ".each()", but it takes the return value and accumulates an array. The final ".get()" is necessary to get a "raw" array instead of a jQuery object (a wrapper around the array), but that's not always necessary; it depends on what you want to do with the result.

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

Comments

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.