I’m trying to make a form’s selected element change its background color upon click. When the page initially loads the first option is selected. I’m trying to figure out what is wrong with the code I have, because it loads CORRECT when its viewed in our page editor (we’re using wordpress & elementor to build our pages and running this as custom html code), but doesn’t not load correctly on a “live” page.
I’ve researched for other methods of doing this without much success, and am especially confused considering the page works – but only when viewed as an admin inside a page editor (elementor).
https://jsfiddle.net/ncLk85mb/
function toggleClass(el) {
var kids = document.getElementById('menu1').children;
for (var i = 0; i < kids.length; i++) {
kids[i].className = "item";
}
el.className = "item highlight";
}
Above is the code I’m attempting to use to do the highlighting. I’ve pasted the entirety of what we’ve got so far into jsfiddle at the link above.
3
Answers
Use
setAttribute
:You don’t need to add another function to add or remove
highlight
class. You already triggerclick
event on yourdiv
element so you have to just modify it like below –JSFiddle Link
Use
element.classList.add()
instead.