I want to hide my drop down menu, to the right so it comes out when the settings logo is clicked on but for some reason it is not working. If anyone can take a look at my code and see what the problem is it would be a great help.
function Startgame() {
var x = Math.floor(Math.random() * 13) + 1;
document.getElementById('computer').src = 'card' + x + '.gif';
document.getElementById('startButton').disabled = "false";
}
function reset() {
total = 0
document.getElementById('computer').src = 'dieDefault.gif';
document.getElementById('img2').src = 'dieDefault.gif';
document.getElementById('score').innerHTML = "0" + total;
}
function puppies() {
document.getElementById('higher').disabled = "false";
document.getElementById('equal').disabled = "false";
document.getElementById('lower').disabled = "false";
}
/////DROPDOWN MENU/////////////
var menuIsOpen = "false";
window.onload = initialise;
function initialise() {
document.getElementById('togglemenu').onclick = navigate;
}
function navigate() {
var panel = document.getElementById("menu");
if (menuIsOpen) {
panel.style.right = "-40em";
menuIsOpen = "false";
document.getElementById('togglemenu').innerHTML = "Open";
} else {
panel.style.top = "0em";
menuIsOpen = "true";
document.getElementById('togglemenu').innerHTML = "Close";
}
}
<div id="settings">
<ul id="menu">
<b> Colour</b>
<form action="menu">
<li>
<input type="radio" value="blackbackground">Black BG</li>
<li>
<input type="radio" value="graybackground">Gray BG</li>
<li id="togglemenu">
<img src="settings_img.png" width="60" hieght="60">
</li>
</form>
</div>
<div id="bodydiv">
<h1> Card Game</h1>
<img src="back.gif" id="computer">
<div id="comp">Computer</div>
<div id="arrow">
<img src="arrow2.png" width="100" height="100">
</div>
<img src="back.gif" id="player">
<div id="play">Player</div>
<div id="kittens">
<button id="startButton" onclick="Startgame();">Start Game</button>
<div id="buttons_1">
<button id="higher" onload="puppies();">Higher</button>
<button id="equal">Equal</button>
<button id="lower">Lower</button>
</div>
<button id="draw" onclick="Startgame();">Draw your card</button>
<div id="resetscore"> <a href="url">Reset Score </a>
</div>
</div>
<div id="score"></div>
</div>
-
1What does "not working" mean? What behavior is it exhibiting?Jonathan M– Jonathan M2014年12月09日 17:24:16 +00:00Commented Dec 9, 2014 at 17:24
-
I want the drop down to hide when the page is loaded, and when the settings logo is clicked on for the drop down to appear. Sorry for the terrible explanationMysteryX– MysteryX2014年12月09日 17:31:15 +00:00Commented Dec 9, 2014 at 17:31
-
What part of this html is (should be) "dropdown"?Andriy F.– Andriy F.2014年12月09日 17:50:20 +00:00Commented Dec 9, 2014 at 17:50
3 Answers 3
Your code is bleeding inconsistencies and errors, so I'm not even sure where to begin.
A couple of things:
if you want to check a variable if it's true, its value should be boolean (
trueorfalse), not text ("true"or"false"); if you put the boolean between quotation marks, it becomes text and you won't be able to check against it like you do it. So if you sayvar value = "true"then checking against it would beif (value == "true")and notif (value). Latter one would only work if you setvar value = true(without the quotation marks).You have to set your element's basic CSS value in order
topto work, ie, set it toposition:relative; top:-40em(hidden by default).Replace
document.getElementById('togglemenu').onclick = navigate;withdocument.getElementById('togglemenu').onclick = function() { navigate() };You won't need to
initializeif you set the CSS properly to hide the menu initially.
Here is the updated code removing fluff, as it was irrelevant:
var menuIsOpen = false;
document.getElementById('togglemenu').onclick = function() { navigate() };
function navigate() {
var panel = document.getElementById("menu");
if (menuIsOpen) {
panel.style.top = "-40em";
menuIsOpen = false;
document.getElementById('togglemenu').innerHTML = "Open";
} else {
panel.style.top = "0em";
menuIsOpen = true;
document.getElementById('togglemenu').innerHTML = "Close";
}
}
#menu {
position:relative;
top:-40em;
}
<div id="settings">
<ul> <b> Settings</b>
<li id="togglemenu">
<img src="http://placehold.it/140x100" width="60" hieght="60" />
</li>
</ul>
<ul id="menu"> <b> Colour</b>
<form action="menu">
<li>
<input type="radio" value="blackbackground" />Black BG</li>
<li>
<input type="radio" value="graybackground" />Gray BG</li>
</form>
</ul>
</div>
Comments
You could probably use JavaScript for this, have some sort of script to hide the menu and then when the user clicks on your logo use onclick to call a function to show your div. Your code doesn't help much, and I'm still very confused as to what is not working.
Take a look at this here