jQuery - dynamic variables?
Posted
by user331884
on Stack Overflow
See other posts from Stack Overflow
or by user331884
Published on 2010-05-26T23:58:10Z
Indexed on
2010/05/27
0:01 UTC
Read the original article
Hit count: 502
Newbie question. Given the following html.
<div id="mycontainer1" class="container">
<input type="text" class="name"/>
<input type="text" class="age"/>
</div>
<div id="mycontainer2" class="container">
<input type="text" class="name"/>
<input type="text" class="age"/>
<input type="text" class="address"/>
</div>
I'm trying to create a function where I can pass an element id and an array that contains the classes of the input values I want to get.
So for example,
var inputClasses = ['name','age'];
getInputValue('#myContainer1', inputClasses);
function getInputValue(elem, arr) {
var temp = {};
$(elem).each(function() {
// need a way to map items in array to variables
// but how do I do this dynamically?
var nameValue = $(this).find('.name').val();
var ageValue = $(this).find('.age').val();
});
© Stack Overflow or respective owner