0
What is wrong with my code.. (JAVASCRIPT)
help make the dropdown function. https://code.sololearn.com/WN2E8uVTIcuH/?ref=app
7 ответов
+ 4
There are a few problems with your code.
First, you implement var x within your function. so every time you click on it, you reinitialize your variable, so even if we fix your menu to open up, it will never close.
Also, you do not need to end } with ;
Finally, you need to specify "document.getElementById" and your element needs to be within "" - as the variable below.
I hope this helps. :)
Here is how I would have implemented it:
function dropdown(){
var x = document.getElementById("text");
var my_style = x.style.visibility;
if(my_style == "hidden"){
x.style.visibility = 'visible';
}else{
x.style.visibility = 'hidden';
}
}
+ 2
var x = 0;
function dropdown(){
var drop= document.getElementById("text");
if(x==0){
drop.style.visibility = 'visible';
x++;
}
else if(x==1){
drop.style.visibility = 'hidden';
x--;
}
};
+ 2
To add to the previous response, as @Frederick said, in JS tab, it should be:
function dropdown()
{
var drop=document.getElementById("text");
if(drop.style.visibility=="hidden")
drop.style.visibility = 'visible';
else
drop.style.visibility = 'hidden';
}
Or, replace the <div> #text inline style from visibility:hidden; with display:none; and in JS tab put this:
function dropdown()
{
var drop=document.getElementById("text");
if(drop.style.display=="none")
drop.style.display = 'block';
else
drop.style.display = 'none';
}
+ 2
The code that you currently have is almost functional. check lines 7 and 8 though.
line 7
else-if should be not have the hyphen/dash and should be
else if with a space.
on line 8 you have 2 double quotes prior to the word test instead of 1
after fixing both of those errors your code ran.
Otherwise, please take a better look at all the variations of the code given in the previous posts as they all should work fairly similarly. The code I posted is not how I would normally do this, but I thought it would closely resemble the code you had enough so that you might be able to understand it a bit easier.
+ 1
@chaoticdawg using ur example im still getting an error .. dropdown () not defined!!!! ... w.t.hell I can clearly see the dropdown () ..I swear im frustrated .. LOL :)
+ 1
I would really appreciate a replication of a working dropdown of my code that I can use as reference. Thanks in advance
+ 1
... I greatly appreciate all your guides and now I can proudly put that in the past.. I know there are options like Bootstrap but ive been having it in mind to try the dropdown menu code.
But I still would appreciate more opinions on the topic.So how would you do it @Taylor :D