Abdul Rahman
Abdul Rahman

Reputation: 1705

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

Upvotes: 5

Views: 123

Answers (4)

amphetamachine
amphetamachine

Reputation: 30623

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

Upvotes: 2

budamivardi
budamivardi

Reputation: 760

use an on click event

if($("input[name='confirmed']").attr("checked")){
   $("input[name='confirmed']").removeAttr("checked");
} else {
   $("input[name='confirmed']").attr("checked","checked");
}

Upvotes: 1

Abdul Rahman
Abdul Rahman

Reputation: 1705

I have changed my jQuery method attr() to prop() with this way:

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

and its working fine. Please see the following link for reference:

http://api.jquery.com/attr

Special thanks to Mr. MelanciaUK by giving his precious hint:

Thank you all for your valuable suggestions.

Upvotes: 0

Quentin
Quentin

Reputation: 943999

Since you are using the function wrong, it doesn't really matter what browser support for your usage is like.

The second argument of the attr method must be a string, a number or a function (which returns a string or a number).

If you want to remove an attribute, use removeAttr

If you want to twiddle the checked status of an input, then use prop not attr (attr sets the default value for the checked state, not the current value).

Upvotes: 1

Related Questions