Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Dec 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How do I loop through ID's to find one that ends with a particular string?

    I have this code that uses the getElementById method. This code works for the specified element ID. I guess my question and challenge is this...how do I get this to loop through all the ID's, to find the one that ends with "_UploadMultipleLink".
    Code:
    <script Language="Javascript">
      var currentHost = window.location.host;
      if (currentHost.indexOf('spepnet') >= 0)
      {
       var MultipleUploadLink = document.getElementById('ctl00_PlaceHolderMain_ctl01_ctl05_UploadMultipleLink');
       if (MultipleUploadLink != null) {MultipleUploadLink.style.display = "none";}
      }
     </script>
    Please help...I really appreciate the ideas.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,038
    Thanks
    23
    Thanked 589 Times in 588 Posts
    An ID is unique. We don't need to loop through anything to find it, getElementById() does that. Your code does this:
    if we're on the page we want
    Code:
    if (currentHost.indexOf('spepnet') >= 0){
    and
    if the id we want is here
    Code:
     if (MultipleUploadLink != null) {
    Then
    make it go away
    Code:
    MultipleUploadLink.style.display = "none";
    Easier to understand code to do this:
    Code:
    <script>
    var currentHost = window.location.host;
    if (currentHost.indexOf('ost') >= 0){
    	document.getElementById('ctl00_PlaceHolderMain_ctl01_ctl05_UploadMultipleLink').style.display = "none";
    }
    </script>
    Evolution - The non-random survival of random variants.

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    966
    Thanks
    19
    Thanked 211 Times in 209 Posts
    Quote Originally Posted by Spaw View Post
    I have this code that uses the getElementById method. This code works for the specified element ID. I guess my question and challenge is this...how do I get this to loop through all the ID's, to find the one that ends with "_UploadMultipleLink".
    Please help...I really appreciate the ideas.
    if there are more than one
    that end with "_UploadMultipleLink"
    something similar to this ...

    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
      <title></title>
      <script>
    ids=[];
    function findIDs(){
     allIds=document.getElementsByTagName("*");
     for(var i=0;i<allIds.length;i++){
      var a = allIds[i].id.split("_");
      if(a[a.length-1] == "UploadMultipleLink")
       ids.push(allIds[i].id)  
     }
     alert(ids)
    }
    </script>
    </head>
    <body>
    <div id="a_UploadMultipleLink"></div>
    <div id="s_UploadMultipleLink"></div>
    <div id="d_UploadMultipleLink"></div>
    <div id="f_UploadMultipleLin"></div>
    <button type=button onclick="findIDs()">pu****</button>
    </body>
    </html>
    Last edited by DaveyErwin; 12-19-2013 at 05:40 PM.

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,885
    Thanks
    56
    Thanked 539 Times in 536 Posts
    or if you're up for a little querySelectorAll action...

    Code:
    <script>
    ids=[];
    function findIDs(){
     allIds=document.querySelectorAll("div[id$='UploadMultipleLink']");
     for(var i=0;i<allIds.length;i++){
       ids.push(allIds[i].id)  
     }
     alert(ids)
    }
    </script>

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,215
    Thanks
    12
    Thanked 338 Times in 334 Posts
    if there are more than one that end with "_UploadMultipleLink"
    then use a class name and not IDs.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,277
    Thanks
    10
    Thanked 581 Times in 562 Posts
    Quote Originally Posted by Spaw View Post
    I have this code that uses the getElementById method. This code works for the specified element ID. I guess my question and challenge is this...how do I get this to loop through all the ID's, to find the one that ends with "_UploadMultipleLink".
    simple:
    Code:
    document.querySelector("[id$='_UploadMultipleLink']")
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •