Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to iterate through multiple select options with jquery

Tags:

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

like image 217
akano1 Avatar asked Aug 04 '09 13:08

akano1


People also ask

How to iterate through multiple select option with jQuery?

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.

How do I iterate through a Dropdownlist in jQuery?

Answers. $('#DropDownList1 option'). each(function(index, option) { // option will contain your item });

How show multiple selected values in Dropdownlist jQuery?

With jQuery, you can use the . val() method to get an array of the selected values on a multi-select dropdown list.


2 Answers

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

like image 122
karim79 Avatar answered Sep 20 '22 21:09

karim79


for optgroups...

$("select[id^='desu']").children('optgroup').children('option:selected').each(      function(id, element) {         document.write(element.title);     } ); 
like image 37
montana Avatar answered Sep 22 '22 21:09

montana