dynamic - checking dynamically named selects using jquery -
how can use jquery value of dynamically named select?
i have several (no exact number) of selects populated this:
<?php ($i = 0; $i < count($manifest); $i++) echo '<select id="headercolumn_' . $i . '"> <option value="">-- column header --</option> <option value="1">option 1</option> <option value="2">option 2</option> <option value="3">option 3</option> <option value="4">option 4</option> </select>'; ?>
later on, have button includes onclick function. when function called, pass how many selects created. now, need value of selects. tried this:
for (var = 0; < valuepassedintofunction; i++) { var thisvalue = $("#headercolumn_" + i).val(); alert("#headercolumn_" + + " = " + thisvalue); }
but when run page, popup this:
#headercolumn_1 = undefined
how can jquery accept dynamic naming convention?
use $.map
function iterate on elements id starting headercolumn_
, , return each elements value array :
var arr = $.map($('[id^="headercolumn_"]'), function(el, i) {return el.value; });
or alert id's , values:
$('[id^="headercolumn_"]').each(function(i, el) { alert(el.id + ' = ' + el.value); });
Comments
Post a Comment