Javascript/Jquery to change class onclick?
With jquery you could do to sth. like this, which will simply switch classes.
$('.showhide').click(function() {
$(this).removeClass('myclass');
$(this).addClass('showhidenew');
});
If you want to switch classes back and forth on each click, you can use toggleClass, like so:
$('.showhide').click(function() {
$(this).toggleClass('myclass');
$(this).toggleClass('showhidenew');
});
Your getElementById
is looking for an element with id "myclass", but in your html the id of the DIV is showhide
. Change to:
<script>
function changeclass() {
var NAME = document.getElementById("showhide")
NAME.className="mynewclass"
}
</script>
Unless you are trying to target a different element with the id "myclass", then you need to make sure such an element exists.
For a super succinct with jQuery approach try:
<div onclick="$(this).toggleClass('newclass')">click me</div>
Or pure JS:
<div onclick="this.classList.toggle('newclass');">click me</div>