How many ways of finding controls through Javascript?

 Posted by vishalneeraj-24503 on 11/19/2013 | Category: ASP.NET Interview questions | Views: 4425 | Points: 40
Answer:

There are following ways to find any controls using javascript :-

1). Simple page ->
var txt_name = document.getelementsbyid('txt_employee_name');

2). Content page ->
var txt_name = document.getelementsbyid('<%=txt_employee_name.ClientId%>');

3). With Ajax ->
var txt_name = $get('txt_employee_name');


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Posted by: Sheonarayan on: 11/20/2013 | Points: 10
Thanks however the answer seems to be incorrect and will throw syntax error as getElementById is the correct function.

Also, the 3rd answer is incorrect as $get is a jQuery function that is used to send request and get response from server. It is not used to find control on the page.

Please let me know if I am wrong.

Thanks
Posted by: vishalneeraj-24503 on: 11/21/2013 | Points: 10
Hi Sheonarayan

It's not getElementById, it's getElementsById.
We can use $get to access or find control when we are using AJAX.

we can also use $get as

var txt_name = $get('<%=txt_employee_name.ClientId%>);
Posted by: Sheonarayan on: 11/21/2013 | Points: 10
Are you sure its "document.getElements ById" not "document.getElementById"? Please cross check one more time. There can't be more than one element having same id so "s" must not be there.

Also what do you mean by AJAX? Are you talking about jQuery? If its jQuery there is no function called $get that finds the element. Please check again.

If you think I am still wrong, please write some demo code and I'll cross check.

Thanks
Posted by: vishalneeraj-24503 on: 11/22/2013 | Points: 10
Hi Sheonarayan,

Sorry i have checked that, It's document.getElementById.
For $get -> I am talking with AJAX. I was using it in VS2005 to find controls and it was working fine.
Posted by: Sheonarayan on: 11/22/2013 | Points: 10
Dear Vishal,

VS2005 is very very old and now days hardly people work on that.

Kindly update your post as per my suggestion.

Thanks

Login to post response