Tuesday 21 January 2014

Read/Get/Find control id using jQuery/JavaScript

So many times we developers needs to read the id of control to get the data.
When the control written in pure HTML then we get the data by simply using
document.getElementById("ControlID")

but when asp.net server control used that control's id is changed when the page
is loaded from the server. At that time it's difficult to get the value.

To solve this problem jQuery has the solid mechanisium to read the control id from page.

*****  Use following for server controls *****

* if you want to use javaScript then use following:
document.getElementById('<%="ControlID".ClientID%>')
e.g. document.getElementById('<%=StudentName.ClientID%>').value

* now if you want to use jQuery then use following:
$("[id$='ControlID']").val()
e.g. $("[id$='StudentName']").val()


*****  Use following for HTML controls  *****

* if you want to use javaScript then use following:
document.getElementById('ControlID')
e.g. document.getElementById('StudentName').value

* now if you want to use jQuery then use following:
$("#ControlID").val()
e.g. $("#StudentName").val()

Hope this will helpful to you.
Happy scripting...

No comments:

Post a Comment