Jump to content

gelElemetsByClassName question


phppup

Recommended Posts

I have used an assortment of document.getElementBy.... to retrieve data

If (document.getElementById('xyz').value >10) { do this... or that.. etc.

I have a group of items with radio buttons and will assign a function if the element ID is A, perhaps a different function if the ID is B, or C etc.

Now I want to assign an element by its CLASS.  How can I incorporate the desire that if the element's CLASS is "bluebackground" then alert("This is of blue background class")

I had tried

if(document.getElementsByClassName("bluebackground") == true) {
alert("message of choice");
}

And other variations, but something is not connecting.

Link to comment
Share on other sites

You know that getElementsByClassName is for searching the page for elements with that class name, right? It's useless for telling you the class of a particular element.

Take the .className of your element, .split() on space, then use .indexOf() to search the array for your class name. It will return a number >=0 if it's in there.

Link to comment
Share on other sites

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.