-1

I am using $(this).removeattr('disabled') to remove the checked attribute from a check box but it is not working in chrome. Can anyone help me for this?

I have also tried some other solution like -

1) $(this).prop('checked', false)

2) $(this).removeAttr('checked')

3) $(this).attr('checked', false)

Thanks in advance.

0xF1
  • 6,046
  • 2
  • 27
  • 50
i_am_beginner
  • 119
  • 3
  • 11
  • 1
    I'm assuming you're using `removeAttr` and not `removeattr` as in your first example. But, why would removing the `disabled` attribute affect the checked attribute of the checkbox? – user229044 Jan 14 '14 at 06:37
  • your *this* must be referring something wrong – Mr. Alien Jan 14 '14 at 06:38
  • You say it doesnt work in chrome. Does it work in another browser? – Realitätsverlust Jan 14 '14 at 06:40
  • what is $(this) in your code, can you show some more of related code..? – Sudhir Bastakoti Jan 14 '14 at 06:40
  • Solution is here: http://stackoverflow.com/questions/426258/how-do-i-check-a-checkbox-with-jquery/426276#426276 – shtrih Jan 14 '14 at 06:43
  • Yes it is working in mozilla browser. Thanks for the comment. BUt as i found it is the problem of post. Actually i am using 3 step form - user submit informationa t first page and come to second page with the post array of first page then select some other field at second page and go to last step. Now if user come back on second page then the user get conflict in the post array of step 1 and step 2. – i_am_beginner Jan 18 '14 at 05:01

1 Answers1

0

First you need to understand difference between "attr" and "prop"

Here's a user's answer, with the main part being:

I'll summarize the main issues:

  • You usually want prop() rather than attr().
  • In the majority of cases, prop() does what attr() used to do. Replacing calls to attr() with prop() in your code will generally work.
  • Properties are generally simpler to deal with than attributes. An attribute value may only be a string whereas a property can be of any type. For example, the checked property is a Boolean, the style property is an object with individual properties for each style, the size property is a number.
  • Where both a property and an attribute with the same name exists, usually updating one will update the other, but this is not the case for certain attributes of inputs, such as value and checked: for these attributes, the property always represents the current state while the attribute (except in old versions of IE) corresponds to the default value/checkedness of the input (reflected in the defaultValue / defaultChecked property).
  • This change removes some of the layer of magic jQuery stuck in front of attributes and properties, meaning jQuery developers will have to learn a bit about the difference between properties and attributes. This is a good thing.

Now there is no function called "removeattr", it should be "removeAttr", this will work if you are using older version of jquery(i.e <1.5).

Since checked is the property of element so its safe and correct to use .prop("checked",false) to make working in all browser.

Another answer gets a bit more practical:

jQuery 1.6+

Use the new .prop() method:

$('.myCheckbox').prop('checked', true);
$('.myCheckbox').prop('checked', false);

jQuery 1.5.x and below

The .prop() method is not available, so you need to use .attr().

$('.myCheckbox').attr('checked', true);
$('.myCheckbox').attr('checked', false);

Note that this is the approach used by jQuery's unit tests prior to version 1.6 and is preferable to using

$('.myCheckbox').removeAttr('checked');
Neithan Max
  • 11,004
  • 5
  • 40
  • 58
Ranjit Singh
  • 3,715
  • 1
  • 21
  • 35
  • Thanks for the comment but i am using jquery 10 and i have used both of the method already. – i_am_beginner Jan 18 '14 at 04:58
  • Yes it is working in mozilla browser. Thanks for the comment. BUt as i found it is the problem of post. Actually i am using 3 step form - user submit informationa t first page and come to second page with the post array of first page then select some other field at second page and go to last step. Now if user come back on second page then the user get conflict in the post array of step 1 and step 2. – Vipul Jain 19 secs ago – i_am_beginner Jan 18 '14 at 05:01