I was just wondering if it's possible to go through multiple select options and get their values and text(if one is selected get the value and text, if 2 is selected get both of their values and text and so on)
I have 15 select boxes in one page?
any help would be appreciated.
<form> <select class="select" name="select3" id="select3"> <option value="0">0</option> <option value="1.99">1</option> <option value="1.99">2</option> <option value="1.99">3</option> <option value="1.99">4</option> <option value="1.99">5</option> <option value="1.99">6</option> <option value="1.99">7</option> <option value="1.99">8</option> </select> </form> <form> <select class="select" name="select" id="select"> <option value="0">0</option> <option value="1.99">1</option> <option value="1.99">2</option> <option value="1.99">3</option> <option value="1.99">4</option> <option value="1.99">5</option> <option value="1.99">6</option> <option value="1.99">7</option> <option value="1.99">8</option> </select> </form>
all the select options have the same class.
thanks
Iterate through <select> options using jQuery Whenever any iterations have to be made, generally forEach(), map(), and filter() methods are used. To traverse the <select> tag, a function has to be created which will return the text, value, or both (text and value) of the options in it.
Answers. $('#DropDownList1 option'). each(function(index, option) { // option will contain your item });
With jQuery, you can use the . val() method to get an array of the selected values on a multi-select dropdown list.
This will alert all the selected options' text and values (for all selects on the page):
$('select > option:selected').each(function() { alert($(this).text() + ' ' + $(this).val()); });
See Core/each and Selectors/selected:
http://docs.jquery.com/Core/each
http://docs.jquery.com/Selectors/selected
for optgroups...
$("select[id^='desu']").children('optgroup').children('option:selected').each( function(id, element) { document.write(element.title); } );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With