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

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 846
  • Last Modified:

Javascript: Get Element ID


I'm trying to get the ID of an element.

So for example when my textboxes get generated the look something like this:

<input id="ctl00_ContentPlaceHolder1_R0C3" onclick="javascript:GetId(this)" />

And then I have the following javascript:

function GetId(textbox)
     var id = textbox.id;
     alert('ID: ' + id);  //This works
     alert('ID: ' + id.substring(27));  //This does not work

What I want is to take the ID, and trim off the "ctl00_ContentPlaceHolder1_" so that all I'm left with is "ROC3" held in a variable that I can then continue to do other stuff with in code.
  • 3
1 Solution
erikTsomikSystem Architect, CF programmer Commented:
can you simply do this getElementById
P1ST0LPETEAuthor Commented:

I should have said: I'm trying to get the ID of an element, modify it, and hold it in a variable.
P1ST0LPETEAuthor Commented:
"can you simply do this getElementById"

Well I already know what the element is..........Im passing it in as a parameter for the function.

I'm trying to get the value of the ID, so that I can parse it down to "R0C3", which is telling myself what row and column [of a table] the textbox is in, because I need to grab other values from the same row.
P1ST0LPETEAuthor Commented:
Figured it out:

Just needed to add ".toString()" into the mix:

so alert('ID: ' + id.toString().substring(27));  works like a charm.

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now