DOM Scripting comments: getElementsByClass http://domscripting.com/blog/display/18 The DOM Scripting equivalent of building a better mousetrap is the quest for a good getElementsByClass function. en Scott http://domscripting.com/blog/display.php/18#comment54 Oh, I just learnt that you can do prototyping on DOM node constructors in Safari, so you don’t have to make getElementsByClass an object method (bad practise). Make sure you also add this to the Document prototype.

Document = document.constructor; HTMLElement = document.createElement("x").constructor;

Document.prototype.getElementsByClassName = HTMLElement.prototype.getElementsByClassName = function(class) {

// you know what

}

]]>
Wed, 05 Oct 2005 12:37:12 GMT
Scott http://domscripting.com/blog/display.php/18#comment53 I find it easier to make getElementsByClassName an object method. This way I can use it just like getElementsByTagName, while also reducing the number of required arguments the function needs to be passed.

Object.prototype.getElementsByClassName = function(searchClass) {

var els = this.getElementsByTagName("*");

// rest of function

}

e.g. var african_elephants = document.getElementById("africa").getElementsByClassName("elephant");

]]>
Tue, 04 Oct 2005 05:53:58 GMT
Dustin Diaz http://domscripting.com/blog/display.php/18#comment52 Hey Jeremy, thanks for posting this. This is Dustin ( found you in my rerrers list :) ). That’s great feedback about the foo-bar Lachlan. I must have skipped that possibility.

I agree Jeremy that it is possibly a bit resource intensive… I originally thought of doing a .split(’ ‘), but then you run a resource-intensive for loop nested in a for loop, which in general, I try to avoid loops nested in loops.

The cssQuery thing just came out like a week ago or so, so that’s a possible goodie as well.

]]>
Mon, 03 Oct 2005 17:42:26 GMT
peterned http://domscripting.com/blog/display.php/18#comment51 insertAfter would be the same as an insertBefore on the nextSibling, and if this sibling is non-existent, an appendChild seems to be performed automatically anyway. So, if more browsers would support it, adding insertAfter to a page would be very simple:

HTMLElement.prototype.insertAfter = function(element, insert) { insert.parentNode.insertBefore(element, insert.nextSibling); }

For IE support you could make it a global function (or linked to "document"), since the parentNode you perform it on can also be retrieved from the insert.parentNode… It’s like you said, the gaps are very plugable.

]]>
Mon, 03 Oct 2005 08:35:24 GMT
Dominic Mitchell http://domscripting.com/blog/display.php/18#comment50 If you find yourself needing slightly more functionality than this, then Dean Edwards’ cssQuery is very useful. It lets you select elements in a similiar manner to getElementsByClass, except that instead of just class name, you can use any CSS selector. More details at:

http://dean.edwards.name/my/cssQuery/

]]>
Sun, 02 Oct 2005 10:02:26 GMT
Lachlan Hunt http://domscripting.com/blog/display.php/18#comment49 Oh, your comment system munged the code. There’s supposed to be 2 slashes before the \s, and I left out some quote marks. Hopefully it works this time.

RegExp("(^|\\s)" + className + "(\\s|$)");

]]>
Sun, 02 Oct 2005 05:12:04 GMT
Lachlan Hunt http://domscripting.com/blog/display.php/18#comment48 That RegExp is buggy. Try class="foo-bar" and getElementsByClass(document,"foo",*);

Use this instead: var pattern = new RegExp((^|\s)" + searchClass + "(\s|$));

]]>
Sun, 02 Oct 2005 05:08:22 GMT
Lachlan Hunt http://domscripting.com/blog/display.php/18#comment47 There’s a getElementsByClassName() function defined in the WHATWG’s Web Apps 1 spec, which takes an arbitary number of arguments, each representing one class name. http://www.whatwg.org/specs/web-apps/current-work/#getelementsbyclassname

I’ve already got a prototype implementation that, AFAIK, fully conforms to the spec. I believe it’s still got some Safari bugs in it, but it works in Firefox, Opera and IE. http://lachy.id.au/dev/script/examples/DOM/DOM.js

]]>
Sat, 01 Oct 2005 23:47:54 GMT