Reputation: 243
im having trouble running javascript hide/show div using CSS classes, when i click on the trigger nothing happens. The relevant codes are the following:
HTML
<a href="" onclick="toggleClass(test,hidden)">Click</a>
<div id="test" class="hidden">
test
</div>
CSS
.hidden{
display:none;
}
JS
function toggleClass(eid, myclass) {
var theEle = document.getElementById(eid);
var eClass = theEle.className;
if (eClass.indexOf(myclass) >= 0) {
theEle.className = eClass.replace(myclass, "");
} else{
theEle.className += "" +myclass;
}
}
Upvotes: 1
Views: 153
Reputation: 6713
two issues: you missed a "#" for the href + ' for the test and hidden (must be strings):
<a href="#" onclick="toggleClass('test','hidden')">Click</a>
<div id="test" class="hidden">
test
</div>
Upvotes: 1
Reputation: 7463
im not 100% certain, but im pretty sure you need to enclose the arguments in quotation marks
<a href="" onclick="toggleClass('test','hidden')">Click</a>
<div id="test" class="hidden">
test
</div>
Upvotes: 0