jquery 1.4.2 and chrome.. detecting a check box is checked....How?

138 Views Asked by At

So for reasons beyond my control, I'm using jquery 1.4.2 and chrome..

I'm having a ludicrous amount of trouble trying to get detecting a check box is checked....working...

Here is what I have...

HTMl generated by asp.net Mvc3

<input type="checkbox" name="criteriaVm.WasApproved" id="criteriaVm.WasApproved">

Various versions I've tried

            var ob = $("#criteriaVm.WasApproved");// is valid
            var chk = ob.attr('checked'); // undefined
            chk = ob.prop('checked');//crash
            chk = ob.get(0).checked;//crash
            chk = ob.get(0).is(":checked");//crash
            chk = ob.is(":checked");//undefined
            chk = ob.is(":checked");// always returns false

I've tried others before I started keeping track to make sure I wasn't just going in circles...

Does anyone know what version works on the old school jquery i'm, stuck with?

1

There are 1 best solutions below

1
AudioBubble On BEST ANSWER

var ob = $("#criteriaVm.WasApproved") is does not select the element with id="criteriaVm.WasApproved" (and ob.length; returns 0).

It selects an element with id="criteriaVm" that has a class name class="WasApproved" (which does not exist)

Having a ., [ or ] in an id attribute will always cause problems with jquery selectors, which is why the HtmlHelper methods replace those characters with an _ (underscore) when generating the id attribute.

Change your id attribute to id="criteriaVm_WasApproved" (or anything you like that does not contain invalid characters) and use

<input type="checkbox" name="criteriaVm.WasApproved" id="criteriaVm_WasApproved">

var ob = $("#criteriaVm_WasApproved");
ob.length; // now return 1
var chk = ob.is(':checked'); // or ob.prop('checked'); etc