phppup Posted January 28, 2019 Share Posted January 28, 2019 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 More sharing options...
requinix Posted January 28, 2019 Share Posted January 28, 2019 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 More sharing options...
phppup Posted January 28, 2019 Author Share Posted January 28, 2019 Something else I learned now. I had a hunch, but i thought it was worth a try to see if I could use CLASS in similar fashion to getElementById. It seems the short answer is NO. Thanks. Link to comment Share on other sites More sharing options...
requinix Posted January 28, 2019 Share Posted January 28, 2019 You can, it's just that the way you'd use them won't help you with this problem. Link to comment Share on other sites More sharing options...
phppup Posted January 28, 2019 Author Share Posted January 28, 2019 Already worked around the issue with a simpler and more effective solution. Just wanted to see if I could pull off something fancy. More trouble than it was worth, but a good learning experience. Thanks again. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.