JQuery change event not firing everytime


JQuery change event not firing everytime



So this is a very weird problem and I would get something was wrong if my event was not being fired at all. But I have the following code that calls a function whenever a checkbox is checked:


$('input:checkbox').change(
function(){
filterByOptions();
});



filterByOptions basically adjusts the content on my page according to the checkboxes checked. Now what I want is for this function to be called everytime a checkbox is clicked on. But this is only happening sometimes (I would say like 50% of the times). I tried placing this code in different parts of my page including in a $(document).ready(function() { method and in separate script tags at the end of the page. But the problem persists.


$(document).ready(function() {



Can someone please offer a solution or tell me what I may be doing wrong?





Please post all the relevant code (HTML) so we can attempt to replicate your issue.
– Scott Marcus
Jun 30 at 16:52






Normally there is no issue, but make sure are you not add checkbox dynamically, if yes then you need to bind again the change event otherwise please submit full code so we can find the issue
– Jagjeet Singh
Jun 30 at 16:57





i am generating checkboxes dynamically as well. How do I bind? The code is like 2000 lines long and it wouldn't work on a fiddle without a connection to my localhost database, which is why I did not paste it in the first place. I think you're on to something with the binding thing. Can you explain that a little more on how I could do that?
– Momin Zahid
Jun 30 at 18:53




2 Answers
2



You've indicated in the comments below that you are dynamically creating new elements and this would cause issues with event binding. The solution for that is to use event delegation, which is where you set up the event on a high level ancestor element the will always exist in the document and then, because most events bubble up to their ancestors, you handle the event there. But, when handling the event, you can examine the element where the event originated and make decisions based on that.



Next, don't use the change event with check boxes. Use the click event. The change event is meant to signal a change to the element's value, when you click on a checkbox, you aren't changing its value, you are changing its checked property. To more accurately trap a click, use the click event.


change


click


change


value


value


checked


click



And yes, the code should be inside of a document.ready() callback.


document.ready()



Also, it makes no sense to have your callback function consist solely of a call to another function. Just make filterByOptions the callback.


filterByOptions



Lastly, JQuery recommends the use of the on() method for event binding:


on()




$(function(){
// Set up the event on the ancestor
$('#parent').on("click", filterByOptions);

// Now, lets dynamically create a new checkbox.
// This element will be inserted into the element that is set up to handle bubbled events,
// so the new element will immediately respond to events even though it wasn't specifically
// bound.
$("#parent").append('<input type="checkbox" name="chk4" value="4" id="chk4" class="handle"><label for="chk4">Box 4</label>');

// All event handlers are automatically passed a reference
// to the event object that triggered them, it is a good idea
// to set up a named argument to reference it.
function filterByOptions(evt){
// Check to see if the event originated at an element you care about
// This is easily accomplished by checking for the class we set up earlier
if(evt.target.classList.contains("handle")){

// Do whatever you need to here, including checking if the box is or isn't checked
console.log(evt.target.name + " was clicked and is now" + (evt.target.checked ? " " : " not ") + "checked.");
}
}

});


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="parent">
<!-- If we give the elements that need handling a common class, it will be easier later-->
<input type="checkbox" name="chk1" value="1" id="chk1" class="handle"><label for="chk1">Box 1</label>
<input type="checkbox" name="chk2" value="2" id="chk2" class="handle"><label for="chk2">Box 2</label>
<input type="checkbox" name="chk3" value="3" id="chk3" class="handle"><label for="chk3">Box 3</label>
</div>





This does not fix my problem. I'm still sometimes not getting the desired result everytime. I am generating my checkboxes dynamically using JS. Does doing it this way bind the future elements to this event as well?
– Momin Zahid
Jun 30 at 18:58





Ok, you didn't mention that you were dynamically binding new checkboxes and the way you wrote your question made it seem like existing checkboxes sometimes triggered the event and sometimes didn't. I've updated my answer to show how events should be bound in your case.
– Scott Marcus
Jul 1 at 18:08





Thank You. I wasn't aware that it even made a difference until I put up this question and researched into what you said about using .on() method. :)
– Momin Zahid
Jul 1 at 20:22



maybe can you double check are you imported jquer js file ? you may check in the log see got error return or not. Hope i can help on





If the file wasn't imported, then the event would never fire. This is not an answer. It should be a comment.
– Scott Marcus
Jun 30 at 16:59






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

C++ thread error: no type named ‘type’ MINGW

Decreasing a static image progress bar horizontally in SDL

How to input without newline? (Python)