jQuery – Check if an option is selected in Select Box

The following HTML is an example to check if an option is selected in a select box.

<html>
  <head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.js"></script>
    <script type="text/javascript">
      $(document).ready(function() {
        $("#select-box").change(function() {
          if ($("#select-box option[value='3']").attr('selected')) {
            alert('Ciao monde');
          }
        });	
      });
    </script>
  </head>
  <body>
    <select id="select-box">
      <option>Select an option</option>
      <option value="1">no alert</option>
      <option value="2">no alert too</option>
      <option value="3">alert</option>
    </select> 
  </body>
</html>

 

Reference:

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.