tags:

views:

2079

answers:

5

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

+7  A: 

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

karim79
A: 

This function will return an array of text/value pairs for the selects matching the given class.

function getSelects(klass) {
    var selected = [];
    $('select.' + klass).children('option:selected').each( function() {
         var $this = $(this);
         selected.push( { text: $this.text(), value: $this.val() );
    });
    return selected;
}
tvanfosson
A: 
//Another option

var selected = [];

$('select :has(:selected)').each( function(){

    var $this = $(this);
    selected.push( { text: $this.text(), value: $this.val() );

});

return selected;
redsquare
+1  A: 

If all of your select boxes start with a similar id ("select_1", "select_2", "select_3", etc), you can just do:

var arr = [];
$("select[id^='select_']").children('option:selected').each(function(){
  //you will do this once for every selected item...
}

This allows you to loop through only specific select boxes, in case you have multiple groupings.

Mike Trpcic
Thanks for the reply. can I use a .change function as well in the code above, like.... .change.each(function() {
amir
well if you wrap the change in an anonymous function yes.change( function(){ $('select').each.....});
redsquare
A: 

for optgroups...

$("select[id^='desu']").children('optgroup').children('option:selected').each( function(id, element) { document.write(element.title); //or whatever });

montana