0

I am trying to sum a 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 does not function as it tries to resolve an input with the name "cost[*]". Ideally I would like to iterate over any element in the cost array. Is there a preferred way of doing this with jQuery? I have other elements in my form that use the radio type so selecting radios in general is not a valid option.

3 Answers 3

6

Make the attribute selector the "starts with" selector (^=):

$('input[name^="cost"]').each(function() {
    ...
});

If you find that you have other input elements that start with "cost" or even "cost[", then perhaps you want to think about changing the way you're querying for the elements. One alternative would be adding a special class name to the elements you're targeting and forget about their names altogether. For 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 then your selector is very simple and very targeted:

$('input.form-cost').each(function() {
    ...
});

You might get the best performance out of simply wrapping the elements in a container with a unique id or class name, and querying for input elements that it contains (as suggested by Allende in the 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() {
    ...
});
Sign up to request clarification or add additional context in comments.

7 Comments

Damn you was faster than me. haha +1
Could you elaborate as to why using a start of the line match versus a glob "input[name*='cost[']" match as illustrated below would be beneficial? I wonder if this method would have issue if I had other fields with "cost" as the first four characters of their name.
@Benji: Good point. You might want to add a trailing '[' to avoid clashing with other inputs. If necessary, you could restrict it further by changing the selector to input[type="radio"][name^="cost["]. Generally a "starts-with" comparison algorithm is faster than a "contains" algorithm, which is why I suggested it.
If you are trying to count the amount of input you have you could add a counter to your function like so: .each(function(counter) {
I think add an specific class or better yet to wrap them in a div will prevent the issue about naming collision and also probably will improve the performance (if radios are wrapped in a div you have no need to deal with selector expressions).
|
2

Try with contains selector in jquery

$("input[name*='cost[']").each(function(){});

Comments

1

Try:

var sum = 0;
$("input[name^='cost']").each(function() {
     sum += Number($(this).val());
});

About "Starts With Selector": [name^="value"]
https://api.jquery.com/attribute-starts-with-selector/

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.