I am brand new to javascript so please bear with me. I am trying to get a span to appear on click which I have accomplished, however I need it to return to being hidden once another link is clicked. This is what I have so far.
<html>
<head>
<style>
aside.apps{
position:relative;
}
span.socialApp{
visibility:hidden;
position:absolute;
top:20px;
left: 0;
background-color:#e9e9e9;
}
</style>
<script>
var state = 'hidden';
function showApp(a) {
if (state == 'visible') {
state = 'hidden';
}
else {
state = 'visible';
}
if (document.getElementById && !document.all) {
x = document.getElementById(a);
x.style.visibility = state;
}
}
</script>
</head>
<body>
<aside class="apps">
<a href="javascript://" onclick="showApp('app1');">link1</a>
<span class="socialApp" id="app1">stuff goes here1</span>
<a href="javascript://" onclick="showApp('app2');">link2</a>
<span class="socialApp" id="app2">stuff goes here2</span>
<a href="javascript://" onclick="showApp('app3');">link3</a>
<span class="socialApp" id="app3">stuff goes here3</span>
<a href="javascript://" onclick="showApp('app4');">link4</a>
<span class="socialApp" id="app4">stuff goes here4</span>
</aside>
</body>
</html>
Currently when link1 is clicked app1 appears, then once link2 is clicked app2 appears over top of link1. When link2 is then closed link1 is still visible. I need to check all 4 and make all hidden except the current selection.
and welcome to Stack Overflow!
First off, I am going to suggest you make use of jQuery assist you with your JavaScript. jQuery (and other similar frameworks like Zepto, MooTools and Dojo) irons over some of the cracks in JavaScript such as cross browser inconsistencies and will make things a lot easier. To include jQuery in your project you just need to add the following in your page’s
<head>tag:Now you’ve got access to jQuery you can remove all of the
onclickattributes you added to your<a>tags. The use ofonclicktags is discouraged as it dates back to the early days of web development before DOM Level 1 standard was (almost) agreed upon. Instead it’s suggested you bind to the ‘click’ event – this will help to keep your HTML and JavaScript separated and in turn make your JavaScript easier to read and debug.jQuery makes it really easy to bind a handler (function) to a click event, you just need to use the
onsyntax:jQuery also provides a quick and easy way to show and hide elements on the page via
showandhide, here how it works:The only thing to watch out for here is that when you ‘hide’ an element using jQuery it actually sets the
displayCSS attribute of the element. In your code above, you were hiding elements by toggling thevisibilityattribute.The last part of the answer lies in how we the currently visible element; this can be achieved by adding a new variable which keeps track of which element is being displayed – you can see this in my modified code below.
If you’re keen to learn more about JavaScript development then may I suggest reading Object Orientation JavaScript which provides an excellent introduction to the language and some of it’s quirks.