/    Sign up×
Community /Pin to ProfileBookmark

Urgent help needed

I’m new to javascript and I cant get the click eventlistener to work, its suppose to set the display of the ul in the header to display: block instead of none

heres the html

[QUOTE]

<!doctype html>
<html lang=”en”>

<head>

<meta charset=”utf-8″>
<title> How the web works </title>
<link rel=”stylesheet” href=”css/style.css” />

</head>

<body>
<div id=”page”>

<header>
<h1> Cole Pratt <span id=”h1s”> No.2968615 </span> </h1>
<img src=”images/menuicon.png” alt=”menuicon” width=”40″ id=”menuicon”/>
<ul>
<li><a href=”#”>home</a></li>
<li><a href=”#”>about</a></li>
<li><a href=”#”>bio</a></li>
</ul>
</header>

</div>
<script src=”scripts/mobilemenu.js” type=”text/javascript”></script>

</body>

</html>

[/QUOTE]

the css

[QUOTE]

/* BEGINNING OF STYLESHEET FOR MAIN PAGE */

body {
background-color: rgb(40, 40, 40);
font-size: 16px;
margin: 0 auto;
font-family: helvetica, arial;
}

header {
position: relative;
background: linear-gradient(rgb(40, 40, 140), rgb(50,50,180));
min-height: 40px;
box-shadow: 0px 0px 5px black inset;
}

header h1 {
position: relative;
padding: 10px;
font-style: italic;
font-size: 1.2em;
font-weight: bold;
color: white;
}

#h1s {
font-size: .6em;
}

img {
position: absolute;
top: 0px;
right: 10px;
}

header ul {
position: relative;
display: none;
}

header ul li {
background-color: red;
text-align: center;
height: 20px;
background-color: grey;
box-shadow: 0px 0px 1px black inset;
}

header ul li a {
text-decoration: none;
color: white;
line-height: 20px;
font-size: .6em;
}

[/QUOTE]

and the javascript

[QUOTE]

// SCRIPT TO DISPLAY AND HIDE THE MENU WHILE IN MOBILE FRIENDLY DEVICE

// STEP ONE CREATE GLOBAL VARIABLE SO WE CAN MONITOR THE MENULOGO FOR CLICKS

var menu = document.getElementById(“menuicon”);

function menuclick(){
if (menu.style.display == “none”){
menu.style.display = “block”;
}
}

menu.addEventListener(‘click’, menuclick);

[/QUOTE]

really really need quick help on this as I have an assignment due tomorrow

thanks
cole

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@coleioauthorJan 23.2018 — updated js


// SCRIPT TO DISPLAY AND HIDE THE MENU WHILE IN MOBILE FRIENDLY DEVICE

// STEP ONE CREATE GLOBAL VARIABLE SO WE CAN MONITOR THE MENULOGO FOR CLICKS

var menuButton = document.getElementById("menuicon");

var menu = document.getElementById("navlink");


function menuclick(){

if (menu.style.display == "none"){

menu.style.display = "block";

}

}

menuButton.addEventListener('click', menuclick);

[/QUOTE]
Copy linkTweet thisAlerts:
@SempervivumJan 23.2018 — Unfortunately menu.style.display has a value only when it has been set by Javascript. Query !="block" instead:
var menuButton = document.getElementById("menuicon");
var menu = document.querySelector("header ul");
function menuclick() {
console.log(menu.style.display);
if (menu.style.display != "block") {
menu.style.display = "block";
}
}
menuButton.addEventListener('click', menuclick);
Copy linkTweet thisAlerts:
@SempervivumJan 23.2018 — ... and by use of a few additional lines of CSS and HTML a toggling behaviour can be achieved without any Javascript:
header #togglemenu {
display: none;
}
header #togglemenu:checked + ul {
display: block;
}
&lt;/style&gt;
&lt;header&gt;
&lt;label for="togglemenu"&gt;&lt;img src="images/buttonup.png" alt="menuicon" width="40" id="menuicon" /&gt;&lt;/label&gt;
&lt;input type="checkbox" id="togglemenu"/&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#"&gt;home&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;about&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;bio&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/header&gt;
Copy linkTweet thisAlerts:
@rootJan 25.2018 — Also, <!doctype html> better off as <!DOCTYPE html>
×

Success!

Help @coleio spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...