[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Find clientID for textbox

Posted on 2008-06-18
8
Medium Priority
?
1,214 Views
Last Modified: 2012-08-13
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

0
Comment
Question by:Camillia
  • 4
  • 4
8 Comments
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 21817972
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
 
LVL 7

Author Comment

by:Camillia
ID: 21818272
let me look and post back.
0
 
LVL 7

Author Comment

by:Camillia
ID: 21821909
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 2000 total points
ID: 21826057
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
 
LVL 7

Author Comment

by:Camillia
ID: 21826100
Thanks,,,so you're nice :)  let me try the "this.id" and see. Will post back.

Kamila.
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 21826461
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
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 21877208
I'm glad I could help.  Thanks for the grade, the points and the fun question.

bol
0
 
LVL 7

Author Comment

by:Camillia
ID: 21877546
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question