Jordash
Jordash

Reputation: 3093

How to get the value of every select box of the same class in Jquery

So I have several select boxes like this:

<select class="designer">
<option>300</option>
<option>3422</option>
</select>

<select class="designer">
<option>5645</option>
<option>8323</option>
<option>3920</option>
</select>

All of them are of the class name "designer"

What I want to do is get the currently selected option as a value and add them all into one variable.

e.g.

var finalvalue = $('.designer').val() + $('.designer').val()

So all of the values of the designer select boxes added together to form the final value of the numbers.

Hope that makes sense.

Thanks

Upvotes: 0

Views: 1572

Answers (1)

user578895
user578895

Reputation:

var total = 0;
$('.designer').each(function(){ total+=parseInt($(this).val()); });

Ideally this could be implemented with fold, but JS doesn't have that natively (nor does jQuery). It's simple to implement, however.

Upvotes: 1

Related Questions