Get all elements in div

Discussion in 'JavaScript' started by So1, Mar 31, 2008.

  1. #1
    Hello again. Looking for help. I want to get all links in div. For example:
    
    <div id="div4search">
    <p>text here...</p>
    <a href='#1'>link1</a>
    <span>text...</span>
    <a href='#2'>link2</a>
    <strong>text...text...text...</strong>
    <a href='#3'>link3</a>
    
    </div>
    Code (markup):
    how to collect all objects in array with tag A inside this DIV where id="div4search"?
    getEmelentsByTagName("A") will collect all links on the page. I need links that placed only inside the div.

    Thanks a lot.
     
    So1, Mar 31, 2008 IP
  2. Morishani

    Morishani Peon

    Messages:
    239
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    <script type="text/javascript">
    var div = document.getElementById("div4search");
    var i;
    for (i=0;i<div.childNodes.length;i++)
     {
     alert(div.childNodes[i]);
     }
    </script>
    
    HTML:
    I hope it's clear enough :)
     
    Morishani, Mar 31, 2008 IP
  3. LumberJake

    LumberJake Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    taken from http://www.thescripts.com/forum/thread167283.html

    var someas = div4search.getElementsByTagName('a');
     
    LumberJake, Mar 31, 2008 IP
  4. So1

    So1 Peon

    Messages:
    45
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thanks, guys. I wnated to find out the simplest way.
    div4search.getElementsByTagName('a')
    Code (markup):
    is that what i needed.
     
    So1, Mar 31, 2008 IP
  5. temp2

    temp2 Well-Known Member

    Messages:
    1,231
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    150
    Digital Goods:
    2
    #5
    your code works depend on browsers, every browse have own method to process this :)
     
    temp2, Apr 1, 2008 IP