Jump to content

jQuery on select box change acting strange


slj90

Recommended Posts

So I have a select / drop down box 

<select id="pos">
  <option id="topleft" value="topleft">Top Left</option>
  <option value="topcenter">Top Center</option>
  <option value="topright">Top Right</option>
  <option value="middleleft">Middle Left</option>
  <option value="middlecenter">Middle Center</option>
  <option value="middleright">Middle Right</option>
  <option value="bottomleft">Bottom Left</option>
  <option value="bottomcenter">Bottom Center</option>
  <option value="bottomright">Bottom Right</option>
</select>

When I use the following code it alerts the current value of the select box e.g "topleft" :
 

$('#pos').on('change', function() {
	
		alert( this.value ); 
		
});

But when I add an if function to only prompt the alert if the value is "topleft" it doesn't work:
 

$('#pos').on('change', function() {
	if(this.val() == "topleft"){
				alert( this.value ); 
		}
});

What is wrong?

Thanks

Link to comment
Share on other sites

What is the difference between the two manners in which you are referencing the value??

 

Hint, one of these things is not like the other:

 

 

$('#pos').on('change', function() {
    if(this.val() == "topleft"){
        alert( this.value ); 
    }

});

Link to comment
Share on other sites

What is the difference between the two manners in which you are referencing the value??

 

Hint, one of these things is not like the other:

I'm not sure exactly what you mean but I have jjust changed to 

 

alert( this.val ); 

Which alerts "undefined", which is why it isn't working.

 

When the user selects the  "Top Left" option I want the function to run.

 

Thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.