Forum Moderators: open

Message Too Old, No Replies

Getting the value of a select option - jquery

         

neophyte

11:07 am on Apr 8, 2011 (gmt 0)

10+ Year Member



Hello All -

I'm having trouble getting the value of a select list option with jquery - I'm sure it's just the wrong conditional syntax.

I've been all over the web today on this question and can find some clues (maybe) to what I'm doing wrong, but nothing I've tried works.

What I want to do is determine the value of a particular select. If the value is "00" then I'll trigger an error.

Select code is here:

++++++++++

<select name="numberRooms" class="yellow">
<option value='00'>00</option>
<option value='01 Standard Room'>01</option>
<option value='02 Standard Rooms'>02</option>
<option value='03 Standard Rooms'>03</option>
</select>

++++++++++

jQuery code is here: (I think the problem area is in bold - but it may be elseware)

++++++++++

function checkRooms() {
if ($('select#numberRooms option:selected').val() == '00') {
$('select#numberRooms').css('background-color','#FFCCCC');
return false;
}
else {
$('select#numberRooms').css('background-color','#E4FFCA');
return true;
}
}

++++++++++

Greatly appreciate any guidance on where I'm going wrong.

Thanks to all in advance!

Fotiman

12:53 pm on Apr 8, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



f ($('select#numberRooms').val() == '00') {

The select element is the one to have a value, the options represent just the subset of possible values. Checking the value of the select element should give you the result you're looking for. :)

neophyte

2:22 am on Apr 9, 2011 (gmt 0)

10+ Year Member



Fotiman -

I tried that syntax before and it didn't work so, after your reply, I re-checked my code for that select and - duhhhh - the select NAME was numberRooms and I didn't have an id. Now that I implemented an id of numberRooms it works like a charm!

Thanks again Fotiman!

Is there an easy way to DISPLAY the value of .val() to the screen for debugging purposes?

Fotiman

4:14 am on Apr 9, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You could log to the JavaScript console of your browser (if you don't want something that will actually appear on the page). Or you could use something like YUI 3 Console [developer.yahoo.com]. But that might be somewhat overkill. You could use an alert for simple debugging.