views:

47

answers:

2

Hi,
I am facing a strange behaviour in jQuery 1.4 with the click event. The idea is to click on a drop-down list (change()), which then invert twice a collection of checkboxes by using the click() function of them.
If I have to do so it is because the click on checkboxes update other fields, so I thought instead of inverting the checkboxes checked attribute, let's just call twice the click on all checkboxes.

Although all the checkboxes are correctly inverted twice, the .length I'm using on the click function looses the right count during the operation, and everytime returns 1 more than it should.

To make it clear here's the idea of the code:

//The 'click' function
$('input:checkbox').click(function(){
    $(this).parent().find("input:checkbox:checked").length;//<- this one's not returning the right count
    //some code updating other fields thanks to the 'length' of the checkboxes
});

//The dropdown function
$('select.dropdown').change(function(){
    $(this).parent().find("input:checkbox").click();//Invert the first time
    $(this).parent().find("input:checkbox").click();//Invert a second time
});

When I manually check and uncheck the fields, everything's working perfectly, it's just when the dropdown call it that the length count is wrong.

Here are 2 scenarios to explain even more in depth the issue I'm experiencing:

  1. If I choose something from the dropdown (the length fails) and then uncheck and recheck a checkbox, the count is right.
  2. If I add a buggy code at the end of the click() function, it works fine except raising an error on the javascript debugger console. Btw, I cannot keep this solution because another function fails in this case.


Cheers,
Nicolas.

+1  A: 
patrick dw
But that won't trigger the checkbox's own onclick handler, as the OP requires.
Marc B
@Marc - Perhaps I am misunderstanding the question. I thought the point was how to invert the checkbox state. If OP needs to also run a attached click handler, then the method already being used would be employed.
patrick dw
@Marc: exactly. In this case the click() won't be called and therefore I won't be able to update my other fields. @patrick: this is not only about checking boxes. Cheers.
Nicolas
@Nicolas - I misunderstood. I just gave an update. You can trigger the handler without changing the checked state by using `.triggerHandler()`.
patrick dw
@patrick: Your edit is almost perfect. It updates the count perfectly, but now the other fields I need to update are not working, but I think your idea is the good one. I'll keep you updated. Cheers, Nicolas.
Nicolas
It's actually working perfectly fine, woh. Thanks, Nicolas.
Nicolas
@Nicolas - You're welcome. Glad it worked. :o)
patrick dw
Could you update your question by adding a section at the end to show how you solved the issue - this might help others to understand things better.
belugabob
@Patrick: isn't it the purpose of an accepted answer?
Nicolas
@Nicolas - Yes you're correct, that is the purpose. No need to update your question. Seems clear that the issue was with calling `.click()` twice in order to fire the handler and keep the correct state of the checkbox, and `.triggerHandler()` was the solution.
patrick dw
@Patrick: exactly :)
Nicolas
+1  A: 

Assuming that the checkboxes and the dropdown share the same form, why not do this...

function countCheckedBoxes(theForm){
    theForm.find("input:checkbox:checked").length;//<- this one's not returning the right count 
    //some code updating other fields thanks to the 'length' of the checkboxes 
}); 

//The 'click' function 
$('input:checkbox').click(function(){ 
    countCheckedBoxes($(this.form));
}); 

//The dropdown function 
$('select.dropdown').change(function(){ 
    countCheckedBoxes($(this.form));
}); 

Then you aren't relying on handling click events in a nested manner(which I believe is the problem) and you are running the 'countCheckedBoxes()' function explicitly, instead of relying on a side-effect of the checkboxes being clicked.

belugabob
Hi, actually I thought about this option, but making a countCheckedBoxes function (returning the length then), does not help. I feel like the issue is that at some point jQuerycount the length before the checkbox has the state `checked` which should not be the case.
Nicolas
Not quite sure why the countCheckBoxes function needs to return the count, but this may become obvious if you added the solution to the end of your original question. The issue that you describe in the above comment is exactly what my suggestion was trying to avoid. i.e. Not triggering an event handler to achieve something that's not really event related. The key thing is separating the function called from within the event from the event handler itself, making it possible to do the work required without bringing that particular event into the equation.
belugabob