0

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

1
  • did you try getElementById('ptag') Commented Mar 13, 2014 at 14:29

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry? Your question wasn't really upvote worthy to be honest. I'm not sure you tried to find the solution yourself in anyway before asking here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.