I am designing a website and I want to show the content just when they click on a button and hide when they click again.
I was able to find a solution but I have to write a different function for all buttons. I tried a few things but couldn’t make it. I’ll be so glad if you help me. 😉
I hide the buttons by display: none;
and the button works with this function :
function funcategory() {
var a = document.getElementById("category");
if (a.style.display === "none") {
a.style.display = "block";
} else {
a.style.display = "none";
}
}
I have to write a different function for all button.
is there a way that I use this for all?
I tried this but it didn’t work :
function funcategory(x) {
var a = document.getElementById(x);
if (a.style.display === "none") {
a.style.display = "block";
} else {
a.style.display = "none";
}
}
and here’s the content which has to be shown whrn the button click(and be hiden when clicked again):
<!--category------------------------------------------------->
<div id="category">
<center>
<button id="categorybtn">Actors</button>
<button id="categorybtn">Singers</button>
<button id="categorybtn">Instagram user</button>
<button id="categorybtn">Model</button>
<button id="categorybtn">Others</button>
<button id="categorybtn">XXX</button>
</center>
</div>
here is the main button:
<button id="topbtn" onclick="funcategory()">Category</button
here is the full code:
<!DOCTYPE html>
<html>
<head>
<title>balalar</title>
<style>
body{
background-color: #ff5993; }
#topbtn{
background-color: #bf42f4;
border: none;
padding: 10px;
font-size: 20px;
border-radius: 6px;
margin: 10px;
padding-left: 20px;
padding-right: 20px;
color: #0e0a0e;
cursor: pointer;}
#categorybtn{
background-color: #ff7700;
border: none;
padding: 10px;
font-size: 20px;
border-radius: 6px;
margin: 10px;
padding-left: 20px;
padding-right: 20px;
color: #0e0a0e;
cursor: pointer;}
#category{
display: none;}
#contactus{
background-color: #dddddd;
font-size: 25px;
display: none;}
</style>
<script>
function funcategory() {
var a = document.getElementById("category");
if (a.style.display === "none") {
a.style.display = "block";
} else {
a.style.display = "none";
}
}
</script>
</head>
<body>
<h1 color="#0e0a0e"><center>BALALAR</center></h1>
<center>
<button id="topbtn" onclick="funcontact()">Contact us</button>
<button id="topbtn">Random</button>
<button id="topbtn" onclick="funcategory()">Category</button>
<button id="topbtn">All</button>
<button id="topbtn">Mine</button>
<button id="topbtn">Top</button>
<button id="topbtn">Log in</button>
</center>
<hr color="black" style="height: 3px; width: 1100px"/>
<!--invisible----------------------------------------------->
<!--category------------------------------------------------>
<div id="category">
<center>
<button id="categorybtn">Actors</button>
<button id="categorybtn">Singers</button>
<button id="categorybtn">Instagram user</button>
<button id="categorybtn">Model</button>
<button id="categorybtn">Others</button>
<button id="categorybtn">XXX</button>
</center>
</div>
<!--contact us----------------------------------------------->
<div id="contactus">
<center>
<p>instagram: <a href="https://www.instagram.com/iammgt/?hl=en">@iammgt</a></p>
<p>telegram: @iammgt</p>
<p>phone: 0935-185-1433</p>
<p>phone2: 0990-4631983</p>
<center>
</div>
</body>
</html>
5
Answers
First of all
id
cannot be duplicate. You can use class as an alternative. and usedocument.querySelectorAll
to get all the buttons. Also add an attributedata-type
you can name it any thing but has to havedata-
as prefix &data-button
value will be used to target the section which will be hidden/shown. For example check thedata-type
of the section. After that you can useclassList.toggle
which will hide/remove class to toggle the visibility1) Don’t use the same
id
to define elements, it must be unique in a document..2) You can send the
id
:onclick="funcategory(this.id)"
to differentiate which one you want to manipulate.3) Example of reusable function:
4) You forgot to close
center
tag. Which are obsolete by now, alternatively you could use CSStext-align
: center;
you want to show the content when the Category button is clicked and you want to hide the content if the category button is clicked again. Is that what you want ?
If so instead of checking
you can check as
If you want to avoid javascript all together, you can do this with a check box and some fancy css.
Here we use the
label
tag with an associated check box. Clicking the label toggles the status of the checkbox. We can then use the:checked
pseudo class with~
, the Adjacent sibling selector to toggledisplay:none;
You should also note the
center
tag is obsolete and should no longer be used. Use CSS instead. And as everyone else has mentioned, id must be unique on the page, use classes instead.