views:

432

answers:

4

The code below works in FF, Safari, Chrome. But IE is giving me issues.

When a checkbox is checked, I cannot get IE to detect it.

$("#checkbox_ID").change(function(){

   if($('#'+$(this).attr("id")).is(':checked'))
   { 
       var value = "1"; 
   }
   else
   { 
       var value = "0"; 
   } 
   alert(value);

   return false;
});

Simply, I'm not getting that alert popup, as expected.
I've even tried it this way:

$("#checkbox_ID").change(function(){

   if( $('#'+$(this).attr("id")'+:checked').attr('checked',false))
   { 
       var value = "1"; 
   } 
   else
   { 
       var value = "0"; 
   } 
   alert(value);

   return false;
});

Here's the simple checkbox input: <input class="prod" type="checkbox" name="checkbox_ID" id="checkbox_ID" value="1"/>

Anyone know if IE requires a different jquery method? or is my code just off?

+2  A: 

It'll probably work if you click somewhere else after you click the checkbox. IE's weird like that.

Might try something like:

$(function () {
  if ($.browser.msie) {
    $('input:checkbox').click(function () {
      this.blur();
      this.focus();
});

More info here: http://norman.walsh.name/2009/03/24/jQueryIE

sblom
I know this is pointless to point out (see what I did there? :P) as I'm sure this is only for the OP to test their code. But since you're mapping blur/focus commands to a click action, to trigger a change action, wouldn't it make sense to just use the click action in this case? Or if you really felt this was necessary you should probably call $(this).trigger('change');
Jay
This idea worked great for me. I used it in prototypejs...if (Prototype.Browser.IE){ el.observe('click', function(e) { e.element().blur(); e.element().focus(); });}
apinstein
+1  A: 

The change event only fires when the value changes after the control loses focus. For a checkbox it is better to hook up to the click event.

Here is the documentation on when the onchange event fires: link text

Daniel
Click event isn't _quite_ the same as change. It's usually pretty close, so that can work fine. But there are ways to make a click in IE force change to be called in the event of a bona fide change.
sblom
Here's more information on the change event. http://www.quirksmode.org/dom/events/change.html.
R0MANARMY
This is not true in all cases, please see below quote from jQuery API documents."For select boxes, checkboxes, and radio buttons, the event is fired immediately when the user makes a selection with the mouse, but for the other element types the event is deferred until the element loses focus." Quoted from http://api.jquery.com/change/
Jay
+2  A: 

As noted by sblom with IE7 and below you need the element to blur for this action to fire, later versions of IE and other browsers don't need the element to explicitly lose focus, simply changing should fire this event (as shown in my below example).

I believe that your problem could be that you're trying to access the 'value' variable when it is out of scope.

$("#checkbox_ID").change(function(){
var value;
if($(this).is(':checked'))
{ 
    value = "1"; 
}
else
{ 
    value = "0"; 
} 
alert(value);

return false; 
});

Here is a working preview, notice that because you return false at the end of the function it resets the checkbox to its previous value, as you have canceled the effect of the click.

Jay
A: 

You need to use the propertychange event instead of the change event. Keep in mind that the propertychange event will fire on things other than checked/unchecked. In order to compensate for this, you must check the window.event.propertyName (for IE only):

$(':checkbox').bind($.browser.msie ? 'propertychange': 'change', function(e) {
    if (e.type == "change" || (e.type == "propertychange" && window.event.propertyName == "checked")) {
        // your code here
    }
});
kflorence