469,270 Members | 1,737 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

script that returns elements matching css selectors

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

Kae
Jul 23 '05 #1
2 1150
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
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.

Similar topics

30 posts views Thread by Toni Mcintyre | last post: by
4 posts views Thread by maya | last post: by
3 posts views Thread by peter | last post: by
9 posts views Thread by Harlan Messinger | last post: by
2 posts views Thread by =?iso-8859-1?q?Jean-S=E9bastien?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.