When I run the code the click event works the first time. When I click it more then one time it seems that the click even has unbounded. How do I keep the click event bounded?
var Categories = function() {
$("select[id^='CategoryListBox-']").click(
function() {
SelectedItem = $(this).val();
$(this).parent().load('/Categories/CategoryPicker?CatID=' + SelectedItem);
//send CatID to the requesting page
$('form#SubmitPost').prepend('<input id="CatID" name="CatID" type="hidden" value="' + SelectedItem + '" />');
});
}
Categories();
It looks like you are using AJAX to load new HTML from the server and you’re overwriting the existing select box with a new one. This will remove event listeners. One solution is to use
live():One suggestion: don’t use an attribute selector like this. Give all the relevant select boxes a class like “category” and then do:
It’s much faster.
Lastly, this is a better way of creating your hidden inputs:
The reason being that this creates the DOM element directly rather than interpreting HTML, which again is much slower.