2

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

5
  • Why not change instead of mousedown Commented Jul 28, 2012 at 8:56
  • 1
    Can you post the exact error message you receive? Even if the selector doesn't match anything, mousedown() should still be safe to call. Commented Jul 28, 2012 at 8:56
  • @AlexBall : Even change is the same ..no luck Commented Jul 28, 2012 at 8:58
  • 1
    change name='isClubMember' to name=isClubMember Commented Jul 28, 2012 at 9:00
  • @FrédéricHamidi : Have updated my qn with error. Commented Jul 28, 2012 at 9:00

4 Answers 4

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Hamidi : +1 Excellent.. Yes I am using Prototype with scriptaculous.. How do I overcome this one ? Kindly suggest.
0

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!

Comments

0

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.

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.