-4

I am facing one issue

HTML

<input id="cb" type="checkbox" name="Employment_Staus" value="E" />
<input type="button" value="Check" id="id" />

JQUERY

$('#id').click(function(){
    $('input[type="checkbox"]')[0].setAttribute("checked", "checked");
});

First time when I click on check button, it works!(it check the check-box ), then I manually uncheck the check box!

When I press check button again after uncheck manually , it not work!

I don't want to use attr or prop etc etc !!

Example

http://jsfiddle.net/wL6qr0hp/4/

Shahid Ghafoor
  • 2,991
  • 17
  • 68
  • 123
  • 2
    `$('input[type="checkbox"]')[0].checked = true` or `$('input[type="checkbox"]').prop("checked", true);` – tymeJV Jan 12 '15 at 16:00
  • 6
    Why are you using jQuery and then trying to avoid half of its features? – Quentin Jan 12 '15 at 16:00
  • [Here's a fixed version of your jsfiddle.](http://jsfiddle.net/atw3maj4/) – Pointy Jan 12 '15 at 16:01
  • @tymeJV sorry! `$('input[type="checkbox"]')[0].checked = true` its not adding checked = true attribute in dom!! – Shahid Ghafoor Jan 12 '15 at 16:13
  • @Pointy I don't want to use attr or prop etc etc !! ! I want to see this attribute with element when it checked !! prop / attr not do that [Check this] (http://stackoverflow.com/questions/27679690/attrchecked-checked-attrchecked-true-not-adding-attribute-in-h) – Shahid Ghafoor Jan 12 '15 at 16:18
  • @Quentin when I use prop/ attribute ! I check that check-box, when I see this element using firebug, it not add checked=true attribute in that element! hope u understand !! – Shahid Ghafoor Jan 12 '15 at 16:20
  • 1
    @ShahidGhafoor — That's because Firebug shows the attribute. As I said in my answer, the attribute and the property represent different things. – Quentin Jan 12 '15 at 16:21
  • I want when check-box checked ! this statement `` should become this `` – Shahid Ghafoor Jan 12 '15 at 16:22
  • Your comments don't make any sense. The updated fiddle shows that the button works properly; what difference does Firebug make? – Pointy Jan 12 '15 at 16:30
  • @Quentin the first statement is without checked= true attribute `` and this statement has checked=true attribute in element `` this is the difference! one has checked=true attribute and other one has not!! so, I NEED, WHEN THIS CHECK BOX CHECKED , THEN checked=true must be in this element like `checked=true` complete it should seems following `` hope u got!! – Shahid Ghafoor Jan 12 '15 at 16:38
  • 2
    Then you have to set both, the property and the attribute, and you have to update the attribute whenever the state changes. – Felix Kling Jan 12 '15 at 16:40
  • @Quentin for more detail!! why jsfiddle not showing cheched=true attribute when check-box is checked !! I required that !!! – Shahid Ghafoor Jan 12 '15 at 16:40
  • @ShahidGhafoor — See the first line of my answer. – Quentin Jan 12 '15 at 16:41

1 Answers1

13

The checked attribute sets the default state, not the current state.

Modify the checked property (with .checked = true) instead.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335