javascript - How to select all elements in name array? -
i trying sum collection of radio selections using jquery.
<input name="cost[alpha]" type="radio" > <input name="cost[beta]" type="radio"> <input name="cost[delta]" type="radio"> ... $('input[name="cost[*]"]').each( function() { ... } this not function tries resolve input name "cost[*]". ideally iterate on element in cost array. there preferred way of doing jquery? have other elements in form use radio type selecting radios in general not valid option.
make attribute selector "starts with" selector (^=):
$('input[name^="cost"]').each(function() { ... }); if find have other input elements start "cost" or "cost[", perhaps want think changing way you're querying elements. 1 alternative adding special class name elements you're targeting , forget names altogether. example:
<input name="cost[alpha]" type="radio" class="form-cost"> <input name="cost[beta]" type="radio" class="form-cost"> <input name="cost[delta]" type="radio" class="form-cost"> and selector simple , targeted:
$('input.form-cost').each(function() { ... }); you might best performance out of wrapping elements in container unique id or class name, , querying input elements contains (as suggested allende in comments):
<div id="cost-inputs"> <input name="cost[alpha]" type="radio"> <input name="cost[beta]" type="radio"> <input name="cost[delta]" type="radio"> </div> $('#cost-inputs input').each(function() { ... });
Comments
Post a Comment