Jump to content

Using getElementsByClassName - beginner question


Gleasonator

Recommended Posts

Hi,

 

I'm very new to JavaScript and have found myself needing to select every element by a classname and apply a style to it. Something like:

 

textholder = document.getElementsByClass("redtext");

textholder.style.color = "red";

 

Obviously the above does not work - my question is, how can I get this to work? I'm also aware that the above can be done easily with CSS, but it's the concept I'm trying to grasp here and not the exact example.

 

Thanks. :)

There is a function - getElementsByClassName, but unfortunately, the bestest browser in the whole widest world doesn't support it - yep, IE.

 

Here is a little write up on the matter:

 

http://robertnyman.com/2008/05/27/the-ultimate-getelementsbyclassname-anno-2008/

 

Basically, it's not easy to target all elements with a specific class name across browsers without using a library like jquery.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.