Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Find clientID for textbox

Posted on 2008-06-18
8
1,208 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Make options hidden and show just option that is on 1 29
Jquery 2 23
jQuery Scroll To Top 5 38
Filktering Alphabetically 8 31
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

828 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