<center> has been deprecated meaning that it's no longer wise to use. It's best to adjust position in your CSS file. It's also advised not to use Inline styles like style="" on your elements, you should place those in the CSS File as well. It's not advised to use the onclick event but rather add an event listener.
Here's a JSFiddle for the plain Javascript version of how you can achieve these things: https://jsfiddle.net/u8zc6yfo/2/
window.onload = function () {
var clickableItems = document.getElementsByClassName("clickable");
for (var i = 0; i < clickableItems.length; i++) {
clickableItems[i].addEventListener("click", function () {
toggle_visibility(this.id);
});
}
}
function toggle_visibility(id) {
var getToggleItem = document.getElementById(id).getAttribute("data-toggle");
var e = document.getElementById(getToggleItem);
(e.style.display != "block") ? e.style.display = "block" : e.style.display = "none";
}
And HTML:
<div class="wrapper">
<span id="toggleshowsomething" data-toggle="something" class="clickable center">Show something</span>
<div id="something">
<p>something</p>
</div>
And CSS:
.wrapper {
width: 300px;
margin:0px auto;
}
.clickable {
color: blue;
cursor: pointer;
}
.clickable:hover {
color: purple;
}
#something {
display: none;
}
}
What this, altogether does, is any item assigned with the class "clickable" will have an event listener added to it. When that item is clicked, since you want it to toggle a different element, you can store it in data-toggle:"whateverIdYouWantToPointTo" - It then, through a ternary statement which is:
(boolean check)? true : false ;
determines whether the item with the id listed in data-toggle is hidden. I did this because your code would not take into account if there were no display property set.
(e.style.display != "block") ? //says to check if display is not shown as block
//this is what to do if it returns true
e.style.display = "block";
//show element
:
//this is what to do if it returns false
e.style.display = "none";
//hide element
}
Then for the sake of completeness I did a JQuery mockup which can be found: https://jsfiddle.net/o6wozosu/
window.onload = function() {
$(".clickable").on("click", function() {
var useId = $(this).attr("data-toggle");
$("#" + useId).fadeToggle();
});
}
It's the same thing. You can use the fadeToggle() method to perform an animation in and out, .toggle(); just changes the display abruptly. There is also .animate({}) which takes different css variables and allows smooth animation through JQuery. You can read the documentation on that here: http://api.jquery.com/animate/