![]() |
|
|
|||||||
![]() |
|
|
Thread Tools | Rate Thread |
|
|
PM User | #1 |
|
Regular Coder ![]() Join Date: Mar 2005
Location: Brighton, UK
Posts: 117
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
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;
});
}
Code:
document.getElementsByAttribute('width');
Code:
document.getElementsByAttribute('width', '200');
Code:
document.getElementsByAttribute('width', '200', 'img');
Code:
document.getElementsByAttribute('width', '200', 'img', 'gallery');
|
|
|
|
|
|
PM User | #3 |
|
The thread killer ![]() ![]() Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Rate This Thread | |
|
|