JavaScript alert doesn't show

I am trying to built a select option menu. If the user chooses the first (default) option, the alert window should be shown, but it isn't shown. Need help, thanks!

Here is my html code

 <select name="sets" id ="selectset"> <option value="General">Select Set</option> <option value="The Happy Couple">Ceremony</option> <option value="Ceremony">Ceremony</option> <option value="Lunch">Lunch</option> <option value="Garden">Garden</option> <option value="Longname">This is max length</option> </select> <input type="button" value=" Move Marked to Set">

Here is my javascript code

 $('#movetoset').click(function() { if(document.getElementById("selectset").value == "General"){ alert("Please choose a set"); } }
2

3 Answers

Why aren't you using jQuery for getting the value of the select as well?

change html code to

<input type="button" onclick="checkFunct()" value=" Move Marked to Set">

and add javascript

function checkFunct() { if(document.getElementById("selectset").value == "General"){ alert("Please choose a set"); } }
5

You need to ensure that the JavaScript code you posted is either:

  • In the document ready (or onload) handler, and/or
  • After the HTML in the page source

Otherwise when you try to select the button by its ID the button element won't have been parsed yet so it won't be found and no click handler will be attached.

Try this:

$(function() { // other document ready processing (if any) here $('#movetoset').click(function() { if($("#selectset").val() == "General"){ alert("Please choose a set"); } } // other document ready processing (if any) here
});

(Noting that $(function() {}); is short-hand for $(document).ready(function(){});)

And as long as you're using jQuery, why not use its .val() function?

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like