|
|||||||
| JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...) |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
GetElementByID - Partial IDs
Been wondering about this. With .net and controls that runat server, the ids are often changed to reflect their hierarchy on the control tree for the page. So, a link with the id "lnkNav" might become something like "ctl10_ctl15_lnkNav" when rendered to the browser.
Given this, and that I don't know exactly what the rendered id will be at runtime, if I want to access client side (assume no server side code for this), how can I find the actual id from the partial string lnkNav? Thanks, Paul |
|
#2
|
||||
|
||||
|
This should work
Code:
function getRealId(partialid){
var re= new RegExp(partialid,'g')
var el = document.getElementsByTagName('*');
for(var i=0;i<el.length;i++){
if(el[i].id.match(re)){
alert(el[i].id)
}
}
}
The code will work with reasonable speed if the page has a reasonable number of tags. If too many, the loop should be restricted somehow, for instance if your element with that id is nested in a certain parent (a table, a div, ...something like that) For instance: var el = document.getElementById('myTable').getElementsByTagName('*'); |
|
#3
|
|||
|
|||
|
Thanks Kor!!! I see what you mean about limiting the loop. Problem is that parent or container element ids may also be generated/modified by .net at runtime.
But I think that specifying a tag name should work in most cases. In any cases that I have needed this, I would know the element that I want to get a handle on. So I will try something like: function getRealId(partialid,tagname){ var re= new RegExp(partialid,'g') if (tagname==''||tagname==null) tagname = '*'; var el = document.getElementsByTagName(tagname); for(var i=0;i<el.length;i++){ if(el[i].id.match(re)){ alert(el[i].id) } } } I could also modify the function to give the option for parent id also. This gives me a great starting point. Cheers, Paul |
|
#4
|
||||
|
||||
|
To speed the code, you may also break the loop, if you are looking for a single element
if(el[i].id.match(re)){ alert(el[i].id);break; } |
|
#5
|
|||
|
|||
|
I know you said no-server-side code, but you really should be using the 'ClientID' attribute in .net to get the ID of a control. This way you are guaranteed to get the correct client-side ID.
|
|
#6
|
|||
|
|||
|
Quote:
|
|
#7
|
|||
|
|||
|
Just wanted to say thank you here. I came across this code and this was exactly what i needed for my project.
|
|
#8
|
||||
|
||||
|
Quote:
Code:
function getRealId(partialid){
var re= new RegExp(partialid,'g');
var elems = document.getElementsByTagName('*'), i=0, el;
while(el=elems[i++]){
if(el.id.match(re)){
alert(el.id);
}
}
}
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|