Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Listing All HTML Elements with Specific attributes

Reply
Thread Tools

Listing All HTML Elements with Specific attributes

 
 
Kabindra
Guest
Posts: n/a
 
      01-20-2010
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
 
Reply With Quote
 
 
 
 
Asen Bozhilov
Guest
Posts: n/a
 
      01-20-2010
On Jan 20, 10:07*am, Kabindra <(E-Mail Removed)> 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[i];
if (typeof curr[attr] != 'unknown' && curr[attr] === value)
{
list.push(curr);
}
}
return list;
}

window.onload = function(){
var list = getElementsByAttr('rel', 'tab2', document.getElementById
('div2'));
};

 
Reply With Quote
 
 
 
 
Kabindra
Guest
Posts: n/a
 
      01-21-2010
Thnx ... it was much helpful

On Jan 20, 3:29*pm, Asen Bozhilov <(E-Mail Removed)> wrote:
> On Jan 20, 10:07*am, Kabindra <(E-Mail Removed)> 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[i];
> * * if (typeof curr[attr] != 'unknown' && curr[attr] === value)
> * * {
> * * * * * list.push(curr);
> * * }
> * }
> * return list;
>
> }
>
> window.onload = function(){
> * var list = getElementsByAttr('rel', 'tab2', document.getElementById
> ('div2'));
>
>
>
> };


 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      01-21-2010
On Jan 20, 5:29 am, Asen Bozhilov <(E-Mail Removed)> wrote:
> On Jan 20, 10:07 am, Kabindra <(E-Mail Removed)> 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[i];
> if (typeof curr[attr] != 'unknown' && curr[attr] === value)
> {
> list.push(curr);
> }
> }
> return list;
>
> }


This is more like getElementsByProp.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT: how to get either specific attributes or all elements under a parent element hisachu XML 0 09-01-2010 05:40 PM
XSLT to Remove Elements with Specific Attributes? Aaron Davies XML 4 02-11-2009 04:13 PM
listing all the html links Dado Ruby 6 05-06-2006 09:26 PM
How do you make sure a frameset is loaded? I'm trying to open a frameset in a new window which shows a specific html page in a specific frame ck388 Javascript 1 09-24-2003 08:32 PM
Finding all elements on specific coordinates Rainer Kugeland Javascript 2 07-23-2003 05:46 AM



Advertisments