get all elements to which a CSS selector applies?

P

petermichaux

Hi,

Is there any way to get all elements to which a CSS selector applies?

Something like (surely I'm dreaming)

elements = document.getAllElementsToWhichThisSelectorApplies(".easyMenu
li > a");

Oh how sweet this would be.

Thanks,
Peter
 
B

BootNic

Hi,

Is there any way to get all elements to which a CSS selector
applies?
[snip]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
function turn(){
var elm,i,j,x,y,z;
elm=document.getElementsByTagName('a');
for (i=0, j=elm.length; i<j; i++) {
x=elm;
y=x.parentNode.tagName.toLowerCase();
z=x.parentNode.parentNode.className;
if(y=='li'&&z=='easyMenu'){
x.style.color='#008000';
}
}
}
</script>
<style type="text/css">
..easyMenu li>a{
color:#FF0000;
}
</style>
<title></title>
</head>
<body>
<button onclick="turn()">check</button>
<ol class="easyMenu">
<li><a href="#">yup</a> <span><a href="#">nope</a></span>
<a href="#">yup</a></li>
<li><span><a href="#">nope</a></span></li>
<li><a href="#">yup</a> <a href="#">yup</a></li>
</ol>
</body>
</html>

--
BootNic Saturday, March 18, 2006 5:46 PM

"No man's life, liberty, or property is safe while the legislature is
in session."
*Judge Gideon J. Tucker, 1866.*
 
B

BootNic

Ian Collins said:
BootNic wrote: [snip]
<script type="text/javascript">
function turn(){
var elm,i,j,x,y,z;
elm=document.getElementsByTagName('a');
for (i=0, j=elm.length; i<j; i++) {
x=elm;
y=x.parentNode.tagName.toLowerCase();
z=x.parentNode.parentNode.className;
if(y=='li'&&z=='easyMenu'){
x.style.color='#008000';
}
}
}

That's only looking for specific elements with a single class, the
OP was looking for a means of finding all elements to which a CSS
style applies. This is a little more complex and one would have to
check computed (or currentStyle in IE) style values for all
elements.


An near as I can tell ".easyMenu li>a{}" seem to be a emements that
have a parent li element, of which the li parent has a class of easyMenu.

Well I suppose this did not allow for more then one class. A small
adjustment is in order to allow for more then one class.

Maybe set z=x.parentNode.parentNode.className.match(/\beasyMenu\b/)
and then set the next if statement to if(y=='li'&&z)

Is it more complex then this?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
function turn(){
var elm,i,j,x,y,z;
elm=document.getElementsByTagName('a');
for (i=0, j=elm.length; i<j; i++) {
x=elm;
y=x.parentNode.tagName.toLowerCase();
z=x.parentNode.parentNode.className.match(/\beasyMenu\b/);
if(y=='li'&&z){
x.style.color='#008000';
}
}
}
</script>
<style type="text/css">
..easyMenu li>a{
color:#FF0000;
}
..easyMenu2 a{
background-color:#5F9EA0;
}
..easyMenu3 a{
font-size:1.5em;
}
</style>
<title></title>
</head>
<body>
<button onclick="turn()">check</button>
<ol class="easyMenu3 easyMenu easyMenu2">
<li><a href="#">yup</a> <span><a href="#">nope</a></span>
<a href="#">yup</a></li>
<li><span><a href="#">nope</a></span></li>
<li><a href="#">yup</a> <a href="#">yup</a></li>
</ol>
</body>
</html>

--
BootNic Saturday, March 18, 2006 10:54 PM

A person without a sense of humor is like a wagon without springs,
jolted by every pebble in the road.
*Henry Ward Beecher*
 
P

petermichaux

Good stuff indeed and just as complex as I'd expected! Not an operation
you want to do too often.

But so simple to use and it does not seem slow at all which is good
news.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top