www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 07-25-2006, 09:42 AM
    pkavanagh pkavanagh is offline
    Registered User
     
    Join Date: Jul 2006
    Posts: 3
    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
    Reply With Quote
      #2  
    Old 07-25-2006, 10:15 AM
    Kor's Avatar
    Kor Kor is offline
    Red Devil Moderator
     
    Join Date: Dec 2003
    Location: Bucharest, ROMANIA
    Posts: 11,143
    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)
    }
    }
    }
    Now pass the partial id to the function.

    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('*');
    Reply With Quote
      #3  
    Old 07-25-2006, 10:35 AM
    pkavanagh pkavanagh is offline
    Registered User
     
    Join Date: Jul 2006
    Posts: 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
    Reply With Quote
      #4  
    Old 07-25-2006, 10:42 AM
    Kor's Avatar
    Kor Kor is offline
    Red Devil Moderator
     
    Join Date: Dec 2003
    Location: Bucharest, ROMANIA
    Posts: 11,143
    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;
    }
    Reply With Quote
      #5  
    Old 07-25-2006, 11:16 AM
    StrikeEagle StrikeEagle is offline
    Registered User
     
    Join Date: Jul 2006
    Posts: 1
    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.
    Reply With Quote
      #6  
    Old 07-25-2006, 04:23 PM
    pkavanagh pkavanagh is offline
    Registered User
     
    Join Date: Jul 2006
    Posts: 3
    Quote:
    Originally Posted by StrikeEagle
    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.
    Yep, agreed, but there are cases where I am modifying or working with controls where I do not have access to the source. For this type of case I am trying to kludge together a method for getting access some page elements via Javascript.
    Reply With Quote
      #7  
    Old 01-22-2008, 09:27 AM
    DssTrainer DssTrainer is offline
    Registered User
     
    Join Date: Sep 2006
    Posts: 13
    Just wanted to say thank you here. I came across this code and this was exactly what i needed for my project.
    Reply With Quote
      #8  
    Old 01-22-2008, 09:41 AM
    Kor's Avatar
    Kor Kor is offline
    Red Devil Moderator
     
    Join Date: Dec 2003
    Location: Bucharest, ROMANIA
    Posts: 11,143
    Quote:
    Originally Posted by DssTrainer
    Just wanted to say thank you here. I came across this code and this was exactly what i needed for my project.
    You are welcome. That was an old post. Meanwhile I have learned that a while loop is faster:
    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);
    }
    }
    }
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 03:08 AM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.