0

currently I'm having a problem with my javascript in IE 8. I have two checkboxes, if I check one, the other one is unchecked. This works fine so far. In IE8 the visual change is only rendered, after I do a right click on that site for example. Please see http://jsbin.com/usodik/9 for an example. Anybody any hint to fix this?

Regards

3
  • Then why don't you use radio?
    – mihsathe
    Jul 5, 2011 at 5:02
  • It's only a snippet. It has to be a checkbox :)
    – SaschaJohn
    Jul 5, 2011 at 5:07
  • Nevermind, posting here gave me a fresh view. The problem is caused by using the onchange, which fires later in IE than in FF and Chrome. I'm thinking of just setting the event to onClick
    – SaschaJohn
    Jul 5, 2011 at 5:09

1 Answer 1

0

Use this code:

function togglePrefixVisibility() {
    if (jQuery('#friendlyNames').is(':checked')){
        jQuery('#friendlyNames').attr('checked', false);
    }       
}

function toggleNameVisibility() {
    if (jQuery('#prefixes').is(':checked')){
        jQuery('#prefixes').attr('checked', false);
    }       
}

General guildelines:

  • Always check/uncheck a checkbox with .attr('checked', false) or .attr('checked', true). Don't use attr('checked') or removeAttr(), because attribute values reference to the ones set in html markup, not runtime.
  • Always ask if a checkbox/radio is checked with .is(":checked") for the same reasons

And also use onclick instead of onchange in your checkboxes to call this functions. I tested it and it's working fine.

Hope this helps. Cheers

2
  • Edgar, I had the code like this before. Problem is, that the attribute checked has the values [checked,undefined]. It's not a boolean. Is the use of .is() faster?
    – SaschaJohn
    Jul 5, 2011 at 5:16
  • If you pass a boolean parameter, jquery takes in charge of switching the value of the attribute. You can see it in the jquery attr documentation. Jul 5, 2011 at 6:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.