Listing All HTML Elements with Specific attributes

K

Kabindra

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
 
A

Asen Bozhilov

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'));
};
 
K

Kabindra

Thnx ... it was much helpful

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'));



};
 
D

David Mark

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. ;)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top