leora
leora

Reputation: 196539

how do you read the values of ALL items in a listbox using jquery

I have a listbox, and I want to read all of the items in a listbox using jquery. I also would like to be able to identify for each item if it is selected or not. What is the simplest way of doing this?

Upvotes: 2

Views: 9140

Answers (2)

mVChr
mVChr

Reputation: 50185

Without any other information on how you want the output formatted:

$('#selectId').find('option').map(function() {
    return $(this).val() + ':' + $(this).is(':selected');
});

See example →

Upvotes: 1

Mark
Mark

Reputation: 5720

$('.myListBox option').each(function(index) {
  if ( ($(this).is(':selected')) {
    // do stuff if selected
  }
  else {
   // this one isn't selected, do other stuff
  }
});

Upvotes: 7

Related Questions