Using .load, I’m loading the following content into #step_2 div:
<div id="step_2">
Select a subcategory:
<select id="subcategory_id" name="subcategory_id">
<option value="0">All subcategories</option>
<option value="68">Cool</option>
<option value="15">Cooler</option>
<option value="74">Excellent</option>
</select>
</div>
I’m then trying to detect when user selects something:
$("#subcategory_id").on("change", function(){
alert( 'Success!' );
});
but with no success. Does anyone see what I’m doing wrong? Help greatly appreciated.
NOTES:
Here’s the full CSS path as seen by FireBug:
html.js body div#container div#main form div#step_2 select#subcategory_id
Here’s the full Javascript file, if context matters:
$(document).ready(function() {
$("#category_id").change(function() {
$("#spinner").show();
$("#step_2").load("process.php?selected=category", { value: $(this).val() }, function() {
// Do something to say the data has loaded sucessfully
$("#spinner").hide();
});
});
$("#subcategory_id").on("change", function(){
alert( 'Success!' );
});
});
In this case you need to use
onin a similar way todelegate, the syntax you are using is the replacement forbind.The first selector has to be an element that won’t be replaced. This way, when the event bubbles up to this point it is caught. You then specify the actual element that will trigger the event as the 2nd parameter of
.on.