By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,608 Members | 1,925 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,608 IT Pros & Developers. It's quick & easy.

script that returns elements matching css selectors

P: n/a
Anyone know of a script which returns a list of elements matching a
specified CSS selector?

Kae
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Kae Verens wrote:
Anyone know of a script which returns a list of elements matching a
specified CSS selector?


hehe! I was thinking exactly of how Dean Edwards' IE7 script selects
elements, and low and behold, the man himself has exactly the script I need
- cssQuery().

Thanks anyway.

kae
Jul 23 '05 #2

P: n/a
Kae Verens wrote:
Kae Verens wrote:

Anyone know of a script which returns a list of elements matching a
specified CSS selector?

hehe! I was thinking exactly of how Dean Edwards' IE7 script selects
elements, and low and behold, the man himself has exactly the script I need
- cssQuery().

Thanks anyway.

kae

You may like Dean's script, but if your needs are simpler, here
is a script that returns all the elements with a certain class
name, even if an element has multiple class names. To make it
faster, pass a start element so search below (it currently
starts at body) to reduce the scope of the recursion.

It returns an array of the matched objects:
function getElementsByClassName(c) {
var cArray = [];
var n = document.getElementsByTagName('body')[0];
var z = new RegExp('\\b' + c + '\\b');

function doTree(n) {
if (n.className && z.test(n.className)){
cArray.push(n);
}
for (var i=0, len=n.childNodes.length; i<len; i++) {
doTree(n.childNodes[i]);
}
}
doTree(n);
return cArray;
}

--
Rob
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.