Go Back   CodingForums.com > :: Client side development > JavaScript programming > Post a JavaScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 04-09-2006, 02:50 PM   PM User | #1
Jak-S
Regular Coder

 
Join Date: Mar 2005
Location: Brighton, UK
Posts: 117
Thanks: 0
Thanked 0 Times in 0 Posts
Jak-S is an unknown quantity at this point
getElementsByAttribute() for prototype

This function will return an array of the elements in a page that contain a certain attribute, you can also give it a value that the attribute has to match, a tag name that the element has to match and a parent element.

I know there are other functions for doing this, but this one is written for use with the prototype JavaScript library, in fact its really just a modified version of the getElementsByClassName() function that’s part of prototype.

Code:
document.getElementsByAttribute = function(attribute, value, tagName, parentElement) {
	var children = ($(parentElement) || document.body).getElementsByTagName((tagName || '*'));
	return $A(children).inject([], function(elements, child) {
		var attributeValue = child.getAttribute(attribute);
		if(attributeValue != null) {
			if(!value || attributeValue == value) {
				elements.push(child);
			}
		}
		return elements;
	});
}
Usage is pretty simple, this will return all elements with a width attribute:
Code:
document.getElementsByAttribute('width');
This will return the same as above, but only the elements where the attribute value is 200:
Code:
document.getElementsByAttribute('width', '200');
This will return the same as above, but only the img elements:
Code:
document.getElementsByAttribute('width', '200', 'img');
This will return the same as above, but only the elements that are children of the gallery element:
Code:
document.getElementsByAttribute('width', '200', 'img', 'gallery');
Jak-S is offline   Reply With Quote
Old 04-21-2006, 03:00 AM   PM User | #2
ott
New Coder

 
Join Date: Apr 2006
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
ott is an unknown quantity at this point
Great stuff! Very nice. Done deal
ott is offline   Reply With Quote
Old 04-21-2006, 03:10 AM   PM User | #3
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enough
I wrote a similar script some time back. Didn't think to add filtering based on tag name though.
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards
liorean is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 Off
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:53 AM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.