Find clientID for textbox

I have a textbox and the clientID comes out as :
ctl00_Body_adrBillingAddress_txtPostalCode

And I use it like below. But I dont want to hardcode the clientID. How can I get the ID.
I tried:
alert(doc.getElementsByTagName('txtPostalCode')); // With this I get "object" but when I wanted to look at the "value"..i got an error.

Also tried: alert(getElementById('<%= doc.FindControl(' + txtPostalCode + ').ClientID %>'));






var doc = document.forms[0];
  var msg = "";
if (doc.ctl00_Body_adrBillingAddress_txtPostalCode.value == "")

Open in new window

LVL 8
CamilliaAsked:
Who is Participating?
 
b0lsc0ttConnect With a Mentor IT ManagerCommented:
Thanks for the response.  For a Javascript way to do it you would want something like ...

 <input type="submit" name="ctl00$Body$adrBillingAddress$btnLookupByPostalCode" value="Lookup" onclick="return valSubmit(this.id);" id="ctl00_Body_adrBillingAddress_btnLookupByPostalCode" />

Of course this assumes you want valSubmit to get the ID.  That is the best way to do this in Javascript.

There may be a way to do this in ASP.NET too.  As a zone advisor I could make it so this question shows in that zone too.  However there is a chance it will be overlooked since the question is not new.  If you want me to add it then I can make one other change that will cause the question to be seen my certain Guru experts.  I guess it depends on where you need the ID and if you want the server to try to write it dynamically or have the browser just send it (my method).

Let me know if you have a question about this.

bol
0
 
b0lsc0ttIT ManagerCommented:
Farzadw,

What is the server language or technology that is making the ID?

Let us see the html source for that tag and the script in your second try.  Also the server script for the tag will help.  Thanks!

Using getElementsByTagName() will give you an array or collection and matches the element name or tag.  For example with the html ...

<div name="div1">

... the method would use div not div1 to get that div and any others (e.g. getElementsByTagName('div')).

What is it that needs the alert?  What calls the alert?  If this is a textbox that is part of the form then it is common to have the event calling the script pass the form object or element to the function.  That way it is easier to reference the element or form field you want.  I don't know that this will work in your case without details from you but it is worth looking at.

Let me know if you have any questions or need more information.

b0lsc0tt
0
 
CamilliaAuthor Commented:
let me look and post back.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
CamilliaAuthor Commented:
Ok, here's more info. This is ASP.Net, C#. Again, what I have works but i dont like hardcoding the clientID. At my last job, i used FindControl and that worked...


---1. The button renders like this:
 <input type="submit" name="ctl00$Body$adrBillingAddress$btnLookupByPostalCode" value="Lookup" onclick="return valSubmit();" id="ctl00_Body_adrBillingAddress_btnLookupByPostalCode" />
 
--2. The button in aspx page looks like this:
 <asp:Button ID="btnLookupByPostalCode" runat="server" Text="Lookup"  OnClick="btnLookupByPostalCode_Click"  CausesValidation="false" />

Open in new window

0
 
CamilliaAuthor Commented:
Thanks,,,so you're nice :)  let me try the "this.id" and see. Will post back.

Kamila.
0
 
b0lsc0ttIT ManagerCommented:
Kamila,

It will return the complete ID value so let me know if you want just part.  Also that is Javascript or part of DOM (i.e. the document object model the browser uses) so it is what you should see in the browser source.  If most of that tag is made by ASP.NET then I am not real sure what to do in ASP.NET to produce that result.

Let me know how it works or if you have a question.

bol
0
 
b0lsc0ttIT ManagerCommented:
I'm glad I could help.  Thanks for the grade, the points and the fun question.

bol
0
 
CamilliaAuthor Commented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.