I am novice to jquery and asp.net. I am using the following coding to retrieve value. Why [0] in $(#textbox1)[0].value is used? is there any alternative way to retrieve value?.
$.ajax({
type: "POST",
url: "Default.aspx/GetTextData",
data: "{}",
contentType: "application/json",
dataType: "json",
success: function(msg) {
$("#txtResult")[0].value = msg.d;
}
});
Many Thanks
In the context of your code, it’s not actually retrieving value, it’s setting:
$("#txtResult")[0].value = msg.dYou can use the alternative, jQuery syntax of (as per my comment):
$('#txtResult').val(msg.d)It’s required to use
[0]as the author of that code would like to assign a value to the standardDOMElementof the input, as[0]is used to get the nativeDOMElementfrom that jQuery object. Alternatively you can use:$('#txtResult').get(0).value = msg.dAlthough the previous way is still preferrable