views:

77

answers:

2

The following does not work:

var EtxtDOB = $get('<%=FormView1.FindControl("frmEditPerson").FindControl("EtxtDOB").ClientID %>');

How can I find this nested control in javascript?

A: 
doesn't work. I believe $get and getElementById are close to the same. Although I've seen getelementID work and $get not work on occasion. I believe $get allows just server controls. i can totally be wrong though.
Eric
It won't work unless you replace 'EtxtDOB' with the *ID* of your control. If it is a dynamically generated ID then you need to use code of the form "David" provided you in the comments to your question.
$get() is part of the ASP.NET AJAX js and defined as a shortcut to document.getElementById() - so you have to reference that .js file to use it, I think the ScriptManager also causes it to be included.
jarrett
+1  A: 

I find it's a lot clearer code-wise to explicitly emit the IDs of the controls you want to access via Javascript in the page's code-behind. Something like:

Page.RegisterClientScriptBlock("clientIDs", "var myControlID = '" + myControl.ClientID + "';");

Then you can access this anywhere in your client-side script and it's a lot cleaner:

var ExtODB = getElementById(myControlID);

If you want to get fancy create a utility function that does this for you... or create a custom attribute that automatically does this.

Bryan