CMS Development

DNeubauer
Member

adding onclick to module

SOLVE

Trying to create a hamburger menu but the HTML is not reading my onclick. This is my button: 

 

<button
class="dropbtn-mobile"
onclick="toggleMenu()">

 

And my js: 

 

function toggleMenu() {
console.log('hello');
var menuBox = document.getElementById('dropdown-content-mobile');
if(dropdown-content-mobile.style.display == "block") { // if is menuBox displayed, hide it
dropdown-content-mobile.style.display = "none";
}
else { // if is menuBox hidden, display it
dropdown-content-mobile.style.display = "block";
}
}

 

It says my function is not defined. 

 

0 Upvotes
1 Accepted solution
Anton
Solution
Key Advisor | Diamond Partner
Key Advisor | Diamond Partner

adding onclick to module

SOLVE

Hi @DNeubauer

seems like a pretty "complicated" function for just a toggle effect.

 

Try this(don't forget to load jQuery before this code):

$( ".dropbtn-mobile" ).click(function() {
$( ".dropdown-content.mobile" ).slideToggle( "slow", function());
});

 

 

hope this helps,

 

 

best, 

Anton 

 

---

@dennisedson - jumping in since @Oezcan is on vacation 😁

 

 




check
Did my post help answer your query? Help the Community by marking it as a solution



View solution in original post

3 Replies 3
Anton
Solution
Key Advisor | Diamond Partner
Key Advisor | Diamond Partner

adding onclick to module

SOLVE

Hi @DNeubauer

seems like a pretty "complicated" function for just a toggle effect.

 

Try this(don't forget to load jQuery before this code):

$( ".dropbtn-mobile" ).click(function() {
$( ".dropdown-content.mobile" ).slideToggle( "slow", function());
});

 

 

hope this helps,

 

 

best, 

Anton 

 

---

@dennisedson - jumping in since @Oezcan is on vacation 😁

 

 




check
Did my post help answer your query? Help the Community by marking it as a solution



dennisedson
Community Manager
Community Manager

adding onclick to module

SOLVE

Let him know that he is not being paid while on this vacation 🙃

Thanks,

Dennis




HubSpot Community Developer ShowMake sure to subscribe to our YouTube channel
where you can find the HubSpot Community Developer Show
0 Upvotes
dennisedson
Community Manager
Community Manager

adding onclick to module

SOLVE

@DNeubauer 

I would check out that if statement.  instead of using your declared variable menuBox, you are using an undefined variable dropdown-content-mobile which appears to be a css ID. 

@Oezcan , what do you think?

Thanks,

Dennis




HubSpot Community Developer ShowMake sure to subscribe to our YouTube channel
where you can find the HubSpot Community Developer Show