Guide: Javascript Hide/Show Content Code

AlisonRobin's picture


JAVASCRIPT HIDE/SHOW Code
Fun With Ternary Operators


INTRODUCTION

Up until now I've been using if else statements for the code that toggles visibility in Javascript, but there is a quicker way using a ternary operator.

EXAMPLE


Click this!

And don't worry, you can change the HIDDEN SECRETS! to be anything you want--divs, pictures, text, whatever. You could change this code so that something other than visibility changes. There are a lot of things you can do. Here's an example (You can click this example that changes colors for instance). Make sure you change the id (where it says test) to something different for each instance of this code on your page! Otherwise it will not work!

Here's the code for you to copy and play with:
<a href="javascript:;" onmousedown="document.getElementById('test').style.display=(document.getElementById('test').style.display=='inline')?'none':'inline';">Change display!</a> <span id="test" style="display:none">HIDDEN SECRETS!</span>

Here's how it reads in pseudocode:
When there's a click, go to the element with the id of test and change its display value
if the element with the id of test has a display value of inline then say 'none' but otherwise say 'inline'.


The ternary operator, which is the section with the ? and the : in the code is simplified like this:
if this part is true ? do this thing : but otherwise do this thing

I will do my best to answer any questions you might have or change the code to more suit your needs but I am still learning like you. Smiling

Good Learning Sources:
StackOverflow where people answered a question I had (but I wasn't the asker, I just googled) about the purpose of the question mark
Mozilla Developer Network where they explain it, but this one was kind of complicated for me. Still useful, helped me get the idea.
ITAuthor was the plainest explanation but they still do it in a way that is complicated for me
W3Schools Doesn't have an article on this but I learned other stuff there and it's the best place to be looking for stuff.
littlesinner's picture

God bless you.

God bless you. <3
AlisonRobin's picture

(I did good)?hugme:scoldme

(I did good)?hugme:scoldme

;w; Oh you did very good...

;w;

Oh you did very good... very good indeed!

-showers with love and affection-

Mmm, yes

Mmm, yes <3 Thank you!!
Please disregard any comments made by this account before 2014~ Thanks!
Mjrn's picture

/bookmarks Thank you so much

/bookmarks

Thank you so much for sharing this. ♥
Account previously Mjrn, returned to Veedeer.
AlisonRobin's picture

Yeah I was kind of surprised

Yeah I was kind of surprised that I hadn't seen any guide for this since other people have been using a similar show/hide code on blogs for a while. I assume it was just a case of everybody assuming that someone else was gonna make a guide for it because that seems like the most logical/reasonable/realistic situation.

So when last night I found a totally different way to do the code that is shorter, I figured I can just go ahead and do that myself. Most people on this site have been super helpful when I ask "oh how did you accomplish this?" or "how does this work?" so I have a lot of giving back I need to do.

I think it's important to get all this stuff out so that as many people can use it as possible because in a few small, isolated cases I've observed a kind of elitist attitude about HTML/CSS/JS and I think the best way to fix that is make sure that everyone has access to whatever they want so no one is left in the dark about stuff.

lurvis's picture

This will be useful, thank

This will be useful, thank you! :D
AlisonRobin's picture

No prob, glad this is

No prob, glad this is helpful. Smiling Use it however, I love reading biographies and stuff.

Edit because added some info and an example to the main post. Trying to make this as clear as possible for people new to it.
rabbanim's picture

(No subject)

<:

Magnificent art by 7FinalGirl8 (Hagal)