Search code examples
jqueryhtmlcssdhtmlattr

jQuery attr() function is cross-platform independant isn't


The following jQuery statement is working for me. but i don't know either it is a cross-platform independent or not?

$("input[name='confirmed']").attr("checked",false);

Its working on fire fox latest version. Please correct me if i am wrong? Thanks


Solution

  • As a general rule, JQuery is fully cross-browser compatible. Usually when something doesn't work in one [modern] browser, it won't work in any of them.

    I think the most common syntax for dealing with checkboxes is thus:

    // set checked state
    $("input[name='confirmed']").prop("checked", true);
    
    // remove checked state
    $("input[name='confirmed']").prop("checked", false);
    
    // test checked state
    $("input[name='confirmed']").is(":checked"); // returns boolean
    
    // pull out only checked inputs
    $("input[name='confirmed']").filter(":checked"); // returns collection