Lisa
Lisa

Reputation: 3181

Handle the select on change event in JQuery

I'm developing a web application in PHP and Javascript and I'm trying to run some code on the change of the value of select here's my code, but it isn't working :

 <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script type="text/javascript" >
    $("#projectType").on("change",function() {
      alert( "hello");
    });
  </script>

and here's the select code:

 <select name="projectType" id="projectType" >
    <?PHP foreach($projectType as $projectTypeID => $projectTypeName)
      {echo '<option value="'.$projectTypeID.'|'.$projectTypeName.'">'.$projectTypeName.'</option>';}?>

   </select>

and ideas?

Upvotes: 0

Views: 79

Answers (1)

halabuda
halabuda

Reputation: 384

Make sure your change event isn't assigned until the DOM is ready. You probably want this:

<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript" >
$(function(){
    $("#projectType").on("change",function(){
      alert("hello");
    });
});
</script>

Upvotes: 1

Related Questions