Sreekanth Reddy Sreekanth Reddy - 8 months ago 57
ASP.NET (C#) Question

Differences between 2 statements in Jquery

I tried to make selection of an item in the radiobuttonlist with the help of Jquery.
I had tried with the following statement for a long:

$('#ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder1_RblSendTo input[type="radio"]')[0].attr("checked",true)

But I dint get the selection. After minutes tested with the following :

$('#ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder1_RblSendTo input[type="radio"]')[0].checked = true

It worked.
Unable to know what's wrong with my first statement ?

Mik Mik
Answer Source

In your first statement you get the DOM element with [0]. That element doesn't have a method named attr.

.attr() is a jQuery method that can only be used with jQuery objects. $("input") returns a jQuery object whereas $("input")[0] gets the first DOM element ("node") matched by the jQuery object.

If you drop the [0] in your first statement, it should work.