user3443160
user3443160

Reputation: 106

Javascript:Toggle Function is not working

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 :(

Upvotes: 0

Views: 1161

Answers (1)

Ian Clark
Ian Clark

Reputation: 9357

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

Upvotes: 3

Related Questions