0

Possible Duplicate:
How to get the text of the selected option of a select using jquery?

I have the following script:

<select id = "people">
<option value = "0">Choose</option>
<option value = "1">John</option>
<option value = "2">David</option>
</select>

How Can I use jQuery to detect the value of the option that is currently selected?

i.e. (pseudo-ish code)

if(people.option.text == "Choose"){
   //Alert - Please make a selection
}else{
   //Valid - do something
}

EDIT: code not working

  echo("<script>");
  echo("$(document).ready(function(){");
  echo("$(\"#go\").click(function(e){");
  echo("var selText = $(\"#people option:selected\").text();");
  echo("if(selText == \"Choose\"){");

  echo("}else{");
  echo("window.location.href = \"viewprofile.php\";");
  echo("}");
  echo("});");
  echo("});");
  echo("</script>");

It constantly triggers the else statement and navigates away...

EDIT: Got it working -> changed .text() and val()

Community
  • 1
  • 1
user559142
  • 12,279
  • 49
  • 116
  • 179

6 Answers6

1

From your example looks like you want to get the selected text, not value.

So have this:

var selText = $("#people option:selected").text();
if(selText == "Choose") {
   //Alert - Please make a selection
} else {
   //Valid - do something
}

To get selected value and check it, have such code:

var selValue = parseInt($("#people").val(), 10);
if (selValue == 0) {
    //Alert - Please make a selection
} else {
    //Valid - do something
}
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
0
if ($('#people [value!=0]:selected').length > 0){
    //selected
} else {
    //not selected
}
calumbrodie
  • 4,722
  • 5
  • 35
  • 63
0

How about this:

        $("select option:selected").each(function () {
            if($(this).text()=="Choose") ...
            else ...
        });
Tae-Sung Shin
  • 20,215
  • 33
  • 138
  • 240
  • I need to detect whether the selected value is equal to 'choose'...so just a simple if/else that checks the selected value – user559142 Aug 17 '11 at 12:25
0
if (jQuery('#people option:selected').val()=="Choose"){
}
else{
}
J0HN
  • 26,063
  • 5
  • 54
  • 85
0

Check the value that is selected using val, then take the appropriate action. Shown below in a form submission handler to prevent form submission. You might also want to consider using the validate plugin for this particular case, making the field required, and having the default option have no value.

$(function() {
   $('form').submit( function() {
       var selectedPerson = $('#people').val();
       if (!selectedPerson || selectedPerson == '0') {
          alert('you must choose an option for people');
          return false;
       }
       // continue form validation and submission
   });
});
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
0

To get the value of the selected <option>, use:

$('select').val()

To get the HTML content of the selected <option>, use:

$'select :selected').html()

And to check if a certain <option> is selected, use:

if ($('option').is(':selected')) ...
Abraham
  • 20,316
  • 7
  • 33
  • 39