views:

83

answers:

4

hi all...

i want to select a checkbox when a button is clicked.

<form action="" method="post" id="form2">
    <input type="checkbox" id="checkone" value="one" name="one" />
    <input type="button" value="Click me" id="buttonone"/>
</form>

when i tried the following, the checkbox was not getting selected

$('#buttonone').click(function() {
    $('#checkone').checked=true;
});

then i tried:

$('#buttonone').click(function() {
    document.getElementById('checkone').checked=true;
});

this time the checkbox got selected. why isn't it getting selected with the jquery $ function?

+4  A: 

Try

$('#checkone').attr('checked', true);

or

$('#checkone').get(0).checked = true;

or

$('#checkone')[0].checked = true; // identical to second example

The reason your first code didn't work is because you were trying to set the checked property on a jQuery object which will have no visible effect as it only works on the native DOM object.

By calling get(0) or accessing the first item [0], we retrieve the native DOM element and can use it normally as in your second example. Alternatively, set the checked attribute using jQuery's attr function which should work too.

Anurag
wow....that was a quick reply..thanks for replying so fast...i'll try now
Anish
hey thanks a lot..it's working....can you please explain why
Anish
@anish - added an explanation, please let me know if it's still unclear, and I'll elaborate further
Anurag
@Anurag - thanks a lot. great explanation. does this apply to all properties and methods of javascript?
Anish
+1, but I really don't like the `attr()` version. It's inefficient, more likely to go wrong and perpetuates the confusion over attributes and properties.
Tim Down
@anish - Yes, there is a big difference between object properties and DOM attributes. jQuery tends to blur the distinction beyond recognition at times. @Tim - lately I've been developing the notion that the whole attributes/properties in DOM is a mess. I mean why not make properties synonymous to attributes. `setAttribute(.., ..)` will update the `property` in most cases, but not vice-versa. Honestly, I think seeing properties as a working copy that doesn't sync back with initial attributes is conceptually wrong from the get go, but that's just my opinion.
Anurag
A: 

Try

$('#checkone').attr('checked', true);

You don't have direct access to DOM object properties because jQuery operates on collections ($(selector) is an array). That's why you have functions defined to manipulate the contents of the returned elements.

RaYell
+2  A: 

You need to use .attr() for the jQuery object, like this:

$('#buttonone').click(function() {
  $('#checkone').attr('checked', true);
});

But it's better to do it the DOM way, like this:

$('#buttonone').click(function() {
  $('#checkone')[0].checked = true; //get the DOM element, .checked is on that
});

Or, completely without jQuery:

document.getElementById('buttonone').onclick = function() {
  document.getElementById('checkone').checked = true;
};
Nick Craver
That second example highlights the issue. jQ newcomers sometimes forget they're working with a jQ object, not a DOM object.
jasongetsdown
+1. One of my pet peeves nicely handled :)
Tim Down
@jasongetsdown - jQ newcomers sometimes forget they're working with a jQ object, not a DOM object. nice tip.@Nick Craver - why is $('#checkone')[0].checked = true; more better.will this make it perform fast?
Anish
@anish-m - Yes, it has significantly better performance than `.attr()`...accessing a DOM property is *always* faster than going *through something else* to access it.
Nick Craver
@Nick - accessing a DOM property is always faster than going through something else to access it. thanks a lot for the info
Anish
A: 

try

$('#checkone').attr('checked', true);

cleary googling for "jquery check a checkbox" was the way to go

Stefanvds