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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Feb 2003
    Posts
    345
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy get element's id knowing element's classname?

    Having:

    <div class="myDiv" id="div3245">
    <div class="myDiv" id="div6534">
    <div class="myDiv" id="div1856">

    How can I get every div's id?

    Code:
    for (var i = 0; i < document.getElementsByTagName("DIV").length; i++)
    {
      var thisDiv = document.getElementsByTagName("DIV")[i];
    
      if (thisDiv.className == 'myDiv')
      {
        thisDivID = ??????????
      }
    }

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Code:
    var divs = document.getElementsByTagName("DIV");
    var len = divs.length;
    var thisDiv, thisDivID;
    for (var i = 0; i < len; i++)
    {
      thisDiv = divs[i];
      if (thisDiv.className == 'myDiv')
      {
        thisDivID = thisDiv.id;
      }
    }
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    Regular Coder
    Join Date
    Feb 2003
    Posts
    345
    Thanks
    0
    Thanked 0 Times in 0 Posts


  •  

    Posting Permissions

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