document.getElementsByWhatever(variant whatever, boolean includeTextNodes )

This method will return a collection of objects who's tag name, attribute names, attribute values or (optionally) text nodes match the "whatever" argument.

For example -- lets say you have a bunch of random elements on a page who share a class name of "myClass". Some of the elements are paragraphs, some are spans, and still others are divs. Calling document.getElementsByWhatever("myClass",0) will return a collection of objects who's class name is myClass.

Another example -- You thought symmetrical was spelled with one "m", and throughout an entire project you spelled it that way, only to find after the project was complete and live that you were wrong. Calling document.getElementsByWhatever("symetrical",1) will return a collection of objects that contain the mispelled "symetrical".

One last example -- You need to wrap the title of a book in EM tags throughout the document. Calling document.getElementsByWhatever("Lying Liars",1) will, you guessed it, return a collection of objects that contain the phrase "Lying Liars".

One important thing to note: At the core of this method is document.getElementsByTagName("*"), which is not supported by Safari 1.1 and under. The method will return a single index array with a value of -1 in cases where the wild card argument is not supported.

slayeroffice logo I'm a span
I'm also a span

I'm a random blurb in a p tag.

And I am a random blurb in a blockquote tag.

A Link to slayeroffice.com

This is a paragraph element with some text in it. It also has the phrase "list item" inside.

I'm an EM with a title

Javascript Source:

document.getElementsByWhatever = function(whatever,includeTextNodes) {
	if(!document.getElementsByTagName("*")) {
		// safari has no idea what that asterix means - return an array who's first index is -1
		// so the calling function will know that the browser cant handle this
		return new Array(-1);
	}
	// change "whatever" to lowercase if its not an int.
	if(!parseInt(whatever))whatever = whatever.toLowerCase();
	// this array will contain all of the object references.
	objArray = new Array();
	// loop over the document
	for(_i=0;_i<document.getElementsByTagName("*").length;_i++) {
		// this variable set to 1 when an object is added to objArray so we 
		// dont double/triple up on object references if they meet more than one condition
		inArray = 0;
		// assign this object to a reference variable
		obj = document.getElementsByTagName("*")[_i];
		// check attributes and values for "whatever"
		if(obj.tagName != "!") { // IE freaks out over HTML comments - dont check for attributes if that's what this tag is.
			for(_w=0;_w<obj.attributes.length;_w++) {
				// IE will list all attributes, regardless of if they defined in the HTML, so check that they are defined.
				if(obj.attributes[_w].value) {
					// if the objects attribute name or its value equal "whatever", add it to objArray
					if(obj.attributes[_w].name.toLowerCase() == whatever || obj.attributes[_w].value.toLowerCase() == whatever) {
						objArray[objArray.length] = obj;
						inArray=1;
						break;
					}
				}
			} 
		}
		// check tag names for "whatever"
		if(obj.tagName.toLowerCase() == whatever && !inArray) {
				objArray[objArray.length]=obj;
				inArray=1;
		}
		// check text nodes for "whatever". 
		if(obj.childNodes.length && !inArray && includeTextNodes) {
			for(_w=0;_w<obj.childNodes.length;_w++) {
				if(obj.childNodes[_w].nodeType == 3 && obj.childNodes[_w].data) {
					cData = obj.childNodes[_w].data.toLowerCase();
					if(cData.indexOf(whatever)>-1) {
						objArray[objArray.length] = obj;
						inArray=1;
						break;
					}
				}
			}
		}
	}
	// send the object reference array back to the caller
	return objArray;
}

Demo HTML Source:

<img src="http://slayeroffice.com/gr/office_logo.gif" width="71" height="63" alt="slayeroffice logo" />

<ul class="myUL" id="ul1">
	<li>list item</li>
	<li>This is another list item - it has a title attribute. foo!</li>
</ul>
<span rel="redborder">I'm a span</span><br />
<span>I'm also a span</span>
<p>
	I'm a random blurb in a p tag.
</p>
<blockquote>
	And I am a random blurb in a blockquote tag.
</blockquote>
<p>
	<a class="redborder" href="http://slayeroffice.com">A Link to slayeroffice.com</a>
</p>
<p>
	This is a paragraph element with some text in it. It also has the phrase "list item" inside.
</p>
<em title="I'm an EM">I'm an EM with a title</em>

document.getElementsByWhatever v1.0
last revision: 02.12.2004
steve@slayeroffice.com
http://www.slayeroffice.com