Specifically, an in memory dom of input type checkbox does not receive the 'checked' attribute (or any other checked indicator) in FireFox.
The commented line can be uncommented to see the test pass, but currently you will see the test fail in firefox.
var cb = $('<input type="checkbox" />')
//var cb = $('input');
if (cb.is(':checked')) alert("checkbox says it's already checked");
cb.click();
if (cb.is(':checked')) alert("checkbox clicked correctly!");
else alert("fail!");
Am I doing non-standard things? Any advice? I'm essentially relying on the jQuery .click to actually check my checkbox in the UI (and I don't want to use .attr, .prop, .val because it will break my nice encapsulation that I got going on.)
Use
cb.prop('checked', true)
instead ofcb.click()
to check your checkboxesFiddle updated http://jsfiddle.net/mE3xb/7/
I know this is an old question, but I just stumbled on an answer that worked for me for my unit test:
First, I toggle the checked property:
Then after that, I trigger "just" the click handler (by using triggerHandler it doesn't actually check/uncheck the box again on other browsers)
This simulates an user "clicking" the checkbox, and checking/unchecking it. Not as encapsulated as you might like it to be for your needs (from reading the above comments and answers) but it seems to get a slightly more thorough job done of it than the currently accepted answer.
Since you are using jQuery 1.4.3, it looks like your best bet will be:
jsFiddle Demo - tested in FireFox & Chrome & IE8
.click()
doesn't work in many strange situations in Firefox (it is something inherently wrong/different in that browser). I would assume because it isn't actually available on the DOM yet, so it doesn't know where this event would be. This is one of the many reasons things like.prop()
exist, etc.