Jump to content

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
https://forums.phpfreaks.com/topic/308239-gelelemetsbyclassname-question/
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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.