{Javascript} Comment Visibility Toggle

Unplugged's picture

Toggling Comment Visibility

aka turning comments on/off using Javascript.



Code 1: Simple Toggle On/Off - Default On
<a href="javascript:;" onmousedown="var a=document.getElementById('comments'); if(a.style.display!='none'&&a.style.display!='block') {a.style.display='block';} if(a.style.display == 'none') { a.style.display = 'block'; } else if(a.style.display == 'block') { a.style.display = 'none'; }"> Toggle Comments </a>


Code 2: Simple Toggle On/Off - Default Off
<body onload="document.getElementById('comments').style.display='none';"></body><a href="javascript:;" onmousedown="var a=document.getElementById('comments'); if(a.style.display!='none'&&a.style.display!='block') {a.style.display='block';} if(a.style.display == 'none') { a.style.display = 'block'; } else if(a.style.display == 'block') { a.style.display = 'none'; }">Toggle Comments</a>



Code 3: Two-Button On/Off - Default On
<a href="javascript:;" onmousedown="document.getElementById('comments').style.display='none'; if(document.getElementById('comments').style.display == 'none'){ document.getElementById('comments').style.display = 'block'; } else if(document.getElementById('comments').style.display == 'block'){ document.getElementById('comments').style.display = 'none'; } ">Show comments</a> | <a href="javascript:;" onmousedown="document.getElementById('comments').style.display='block'; if(document.getElementById('comments').style.display == 'none'){ document.getElementById('comments').style.display = 'block'; } else if(document.getElementById('comments').style.display == 'block'){ document.getElementById('comments').style.display = 'none'; } ">Hide comments</a>


Code 4: Two-Button On/Off - Default Off
<a href="javascript:;" onmousedown="document.getElementById('comments').style.display='none'; if(document.getElementById('comments').style.display == 'none'){ document.getElementById('comments').style.display = 'block'; } else if(document.getElementById('comments').style.display == 'block'){ document.getElementById('comments').style.display = 'none'; } ">Show comments</a> | <a href="javascript:;" onmousedown="document.getElementById('comments').style.display='block'; if(document.getElementById('comments').style.display == 'none'){ document.getElementById('comments').style.display = 'block'; } else if(document.getElementById('comments').style.display == 'block'){ document.getElementById('comments').style.display = 'none'; } ">Hide comments</a> <style>#comments{display:none}</style>



Note: Unfortunately the on/off display state resets on each new comment page.

Credit goes to the wonderful AlisonRobin and GMSuerte who helped out a lot with this and did most of the coding, really.
Also credit to Hraeth and Nirvana for the main JS base.
Couldn't have done it without them. You rock.

One Button:
Try it!

Two Button:
Show comments | Hide comments
Unplugged's picture

Test comment ~

Test comment ~

This'll be fun! Thanks, you

This'll be fun! Thanks, you guys.

maannn this is wicked cool

maannn this is wicked cool uvu
Unplugged's picture

Thank you! I hope it'll come

Thank you! I hope it'll come in useful for some.

This is absolutely cool! You

This is absolutely cool! You guys are brilliant
AlisonRobin's picture

Groggy morning track. Was fun

Groggy morning track. Was fun helping with this last night. Smiling

Oh tracking &hearts; ! Thank

Oh tracking ♥ ! Thank you for this!

Sig: Aihnna

RikkaChan's picture

Thank you!

Thank you!

Snazzyyyyy

Snazzyyyyy
LambFleece's picture

Nice!! Thank you! c:

Nice!! Thank you! c:

Avvie by Hadoukin

I adore this.

I adore this.

Thank you, Tuo and Kohva!

/thank you/

/thank you/

Oh my gosh this made my day.

Oh my gosh this made my day.
Aivilo's picture

Cool o3o

Cool o3o
AlisonRobin's picture

So for a long time I thought

So for a long time I thought we couldn't get onload to work but then I talked to the wonderful GMSuerte (who is a totally awesome and wonderfully helpful dude as it turns out, I do not have enough good things to say about him, seriously) and he had a solution, which was to create a second body tag inside of the blog and use onload through it. That way you can use one JS function in onload to assign display:none to the comments, and then you have the other function in the anchor tag as onmousedown to toggle visibility!

<body onload="document.getElementById('comments').style.display='none';"></body><a href="javascript:;" onmousedown="if(document.getElementById('comments').style.display=='none'){document.getElementById('comments').style.display='block';}else if(document.getElementById('comments').style.display=='block'){document.getElementById('comments').style.display='none';}">CHANGE</a>
Unplugged's picture

Alison you and your friend

Alison you and your friend are amazing. That is so clever and so much nicer. Sorry for getting back to this so late but i'll switch up the codes in a moment. I definitely prefer your version.
AlisonRobin's picture

I just met the guy and asked,

I just met the guy and asked, never met him before that day but Hum knows him. He's just that awesome and able to help. GMSuerte is a good person, let it be known!

EDIT: Still been reading up on more stuff and been learning to use variables more efficiently. Been going through my code and making things shorter where I can and did it with the stuff on this page too:

<a href="javascript:;" onmousedown="var a=document.getElementById('comments'); if(a.style.display!='none'&&a.style.display!='block') {a.style.display='block';} if(a.style.display == 'none') { a.style.display = 'block'; } else if(a.style.display == 'block') { a.style.display = 'none'; }">Toggle Comments</a>

So I made the long part with the method just "a" so that instead of typing out the whole thing every time you can just write a.style.display etc. Almost cuts the simple page toggle code by half.
Unplugged's picture

Sorry for the late reply

Sorry for the late reply Alison but as always, super sweet. Smiling I'll update the code again.
_lioness_'s picture

Track.. useful thing

Track.. useful thing
Mest
Shelby