0

What is wrong with my code.. (JAVASCRIPT)

help make the dropdown function. https://code.sololearn.com/WN2E8uVTIcuH/?ref=app

26th Sep 2017, 9:20 PM
Antony O. Onyango
Antony O. Onyango - avatar
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'; } }
26th Sep 2017, 10:16 PM
Frédéric Charette
Frédéric Charette - avatar
+ 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--; } };
26th Sep 2017, 9:53 PM
ChaoticDawg
ChaoticDawg - avatar
+ 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'; }
27th Sep 2017, 12:00 AM
Ipang
+ 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.
27th Sep 2017, 9:23 PM
ChaoticDawg
ChaoticDawg - avatar
+ 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 :)
27th Sep 2017, 8:53 PM
Antony O. Onyango
Antony O. Onyango - avatar
+ 1
I would really appreciate a replication of a working dropdown of my code that I can use as reference. Thanks in advance
27th Sep 2017, 9:06 PM
Antony O. Onyango
Antony O. Onyango - avatar
+ 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
27th Sep 2017, 9:48 PM
Antony O. Onyango
Antony O. Onyango - avatar