Listing All HTML Elements with Specific attributes

Discussion in 'Javascript' started by Kabindra, Jan 20, 2010.

  1. Kabindra

    Kabindra Guest

    How do i list all the HTML Elements inside a DIV with specific
    attributes?

    <div id="div1">
    <a href="#" rel="tab1">Link 1</a>
    </div>
    <div id="div2">
    <a href="#" rel="tab2">Link 2</a>
    <a href="#" rel="tab2">Link 3</a>
    <a href="#" rel="tab2">Link 4</a>
    </div>
    <div id="div3">
    <a href="#" rel="tab3">Link 5</a>
    <a href="#" rel="tab3">Link 6</a>
    <a href="#" rel="tab3">Link 7</a>
    </div>

    from above I would like to get a elements with rel="tab2"
    i.e output should be
    list of a tags inside the div2
     
    Kabindra, Jan 20, 2010
    #1
    1. Advertising

  2. On Jan 20, 10:07 am, Kabindra <> wrote:
    > How do i list all the HTML Elements inside a DIV with specific
    > attributes?


    function getElementsByAttr(attr, value, parent)
    {
    var collection = parent.getElementsByTagName('*'),
    list = [];
    for (var i = 0, len = collection.length, curr; i < len; i++)
    {
    curr = collection;
    if (typeof curr[attr] != 'unknown' && curr[attr] === value)
    {
    list.push(curr);
    }
    }
    return list;
    }

    window.onload = function(){
    var list = getElementsByAttr('rel', 'tab2', document.getElementById
    ('div2'));
    };
     
    Asen Bozhilov, Jan 20, 2010
    #2
    1. Advertising

  3. Kabindra

    Kabindra Guest

    Thnx ... it was much helpful

    On Jan 20, 3:29 pm, Asen Bozhilov <> wrote:
    > On Jan 20, 10:07 am, Kabindra <> wrote:
    >
    > > How do i list all the HTML Elements inside a DIV with specific
    > > attributes?

    >
    > function getElementsByAttr(attr, value, parent)
    > {
    >   var collection = parent.getElementsByTagName('*'),
    >     list = [];
    >   for (var i = 0, len = collection.length, curr; i < len; i++)
    >   {
    >     curr = collection;
    >     if (typeof curr[attr] != 'unknown' && curr[attr] === value)
    >     {
    >           list.push(curr);
    >     }
    >   }
    >   return list;
    >
    > }
    >
    > window.onload = function(){
    >   var list = getElementsByAttr('rel', 'tab2', document.getElementById
    > ('div2'));
    >
    >
    >
    > };
     
    Kabindra, Jan 21, 2010
    #3
  4. Kabindra

    David Mark Guest

    On Jan 20, 5:29 am, Asen Bozhilov <> wrote:
    > On Jan 20, 10:07 am, Kabindra <> wrote:
    >
    > > How do i list all the HTML Elements inside a DIV with specific
    > > attributes?

    >
    > function getElementsByAttr(attr, value, parent)
    > {
    > var collection = parent.getElementsByTagName('*'),
    > list = [];
    > for (var i = 0, len = collection.length, curr; i < len; i++)
    > {
    > curr = collection;
    > if (typeof curr[attr] != 'unknown' && curr[attr] === value)
    > {
    > list.push(curr);
    > }
    > }
    > return list;
    >
    > }


    This is more like getElementsByProp. ;)
     
    David Mark, Jan 21, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aaron Davies
    Replies:
    4
    Views:
    4,350
    The Magpie
    Feb 11, 2009
  2. hisachu
    Replies:
    0
    Views:
    1,565
    hisachu
    Sep 1, 2010
  3. Dado

    listing all the html links

    Dado, May 3, 2006, in forum: Ruby
    Replies:
    6
    Views:
    305
    anne001
    May 6, 2006
  4. Rainer Kugeland

    Finding all elements on specific coordinates

    Rainer Kugeland, Jul 21, 2003, in forum: Javascript
    Replies:
    2
    Views:
    138
    Rainer Kugeland
    Jul 23, 2003
  5. Janis Papanagnou

    Hide all elements of a specific class

    Janis Papanagnou, Jun 15, 2008, in forum: Javascript
    Replies:
    9
    Views:
    223
    Jonas Raoni
    Jun 16, 2008
Loading...

Share This Page