Question

I am trying to make a simple toggle but it is not working.And i want it done in javascript not in jquery. Here is my javascript.

<script>
function showhide() {

    if (document.getElementById(ptag).style.display = "block") {
        document.getElementById(ptag).style.display = "none";
    } else {
        document.getElementById(ptag).style.display = "block";
    }
}
</script>

Here is my HTML.

<input type="button" value="Show hide" onclick="showhide()">
<p id="ptag">Some text here.</p>

I need solution :(

Was it helpful?

Solution

Change your if condition to:

if(document.getElementById("ptag").style.display == "block"){
                           ^^^^ string                ^^^ double equals

And replace all other references of ptag to "ptag"

See working JSFiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top