I am trying to set eventlistener to a button
<button id="btn">click me</button>
document.getElementById('btn').addEventListener('click', function (e) {
test(e.target);
}, false);
And each time click is triggered, the other HTML element(.random ul
) will get a new class of my class
or remove it(on the second click)
function test() {
document.querySelector(".random ul").className = document.querySelector(".random ul").className += ' newclass'
? '' //need to remove the newclass
: ''; //need to add the newclass
}
This obviously not working. I also found the following script to properly remove added classname:
document.getElementById("MyElement").className =
document.getElementById("MyElement").className.replace
( /(?:^|\s)MyClass(?!\S)/g , '' )
//from: http://stackoverflow.com/questions/195951/change-an-elements-class-with-javascript
I really don't know how to make it work and would like to ask you to help me with it please.