Suave Nti
Suave Nti

Reputation: 3739

Jquery Checkbox change event not firing

Am trying to sortout this small issue from past hour :

<script type="text/javascript">
$(document).ready(function(){        
    $("input[name='isClubMember']:checkbox").mousedown(function() {
         if (!$(this).is(':checked')) {
            this.checked = confirm("Are you sure?");
            $(this).trigger("change");
         }
    });
 });
</script>

Funnily this works in JSFiddle ..but not in my jsp page. Sample

The above code gives me null error at this line (in Debug console)

 $("input[name='isClubMember']:checkbox").mousedown(function() {

JQ -Ver : 1.7.2 Browser : IE8

Update : Error in IE console :

 'null' is null or not an object 

at the above mentioned line

Upvotes: 2

Views: 3139

Answers (4)

Fr&#233;d&#233;ric Hamidi
Fr&#233;d&#233;ric Hamidi

Reputation: 262919

It looks like your JSP page includes the Prototype library after jQuery.

Prototype's $() function takes an id and returns null if it cannot find the element, which is consistent with the behavior you're observing.

Try using jQuery instead of $ everywhere in your code:

jQuery("input[name='isClubMember']:checkbox").mousedown(function() {
    // ...
});

Or put your code in a closure that is passed the jQuery object in a $ argument:

(function($) {
    // The rest of your code...
})(jQuery);

Or take advantage of the fact that a ready handler is passed the same $ argument:

jQuery(document).ready(function($) {
    // The code in your 'ready' handler...
});

You may also want to run jQuery in noConflict mode, to avoid overriding Prototype's $() function if the order of inclusion changes.

Upvotes: 7

Krym
Krym

Reputation: 743

I guess the selector is broken in IE8 somehow, try [type='checkbox'], should be faster anyway

    $("input[name='isClubMember'][type='checkbox']").mousedown(function() {

Upvotes: 0

Danil Speransky
Danil Speransky

Reputation: 30453

I think in your page jQuery is not connected. Check.

I think so because $ - function and returns object anyway, but if there is no jQuery then $(str) returns null.

Upvotes: 0

easteregg
easteregg

Reputation: 509

i would suggest, you could add a class name to the checkbox, and find the checkbox by a class name! it seems thats the problem with ie8 in this case!

Upvotes: 0

Related Questions