Solved

Find clientID for textbox

Posted on 2008-06-18
8
1,205 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
Comment Utility
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
Comment Utility
let me look and post back.
0
 
LVL 7

Author Comment

by:Camillia
Comment Utility
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
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 500 total points
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 7

Author Comment

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

Kamila.
0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
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
Comment Utility
I'm glad I could help.  Thanks for the grade, the points and the fun question.

bol
0
 
LVL 7

Author Comment

by:Camillia
Comment Utility
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now