-1

I am truely sorry if this is a repeated question.

I want to set max-height of #menudd.show to satify my transition. I want to do this in javascript so i can specify the value more precisely.

This is what i got and its not working for some reason...

HTML:

<div id="menudd">
    <a href="index.html">Home</a>
    <a href="aboutme.html">About Me</a>
    <a href="shotterm.html">Short-Term</a>
    <a href="middleterm.html">Middle-Term</a>
    <a href="longterm.html">Long-Term</a>
</div>

CSS:

#menudd {
    position: fixed;
    overflow: hidden;
    max-height: 0px;
    opacity: 0;
    width: 200px;
    border-radius: 10px;
    box-shadow: 0 0 35px 15px black;
    transition: all 1s ease;
}

#menudd.show {
    opacity: 1;
}

JavaScript:

$("#menudd." + show).get(0).style.maxHeight = document.getElementById("menudd").getElementsByTagName("A")[0].offsetHeight * document.getElementById("menudd").getElementsByTagName("A").length + "px";

This outputs "show is not defined" in the console.

If i use $("#menudd.show").get(0).style.maxHeight it outputs "cannot read property 'style' of undefined" in the console.

If i use $("#menudd.show").style.maxHeight it outputs "cannot read property 'maxHeight' of undefined" in the console.

Any help is highly appreciated! Good day to you. :)

Lukas Knudsen
  • 197
  • 2
  • 3
  • 14

2 Answers2

0

In your question, you said you wanted to look for an element with class "show" inside a div.

You are currently looking for a variable, and not a class. Change this:

$("#menudd." + show)

To this:

$("#menudd.show")

To set the max height, change it to this:

$( "#menuadd.show" ).css( { "maxHeight": document.getElementById("menudd").getElementsByTagName("A")[0].offsetHeight * document.getElementById("menudd").getElementsByTagName("A").length + "px" } );
Jesse Schokker
  • 896
  • 7
  • 19
0

First, I will add some stuff for readability - like a padding: 0.5em; into you dropdown menu. Also, the <br /> tag after the </a> tags (excluding the last one).

Second, the errors happened because there are no element in the page with the class show. I added it to the <div> of the dropdown to show it working and make the errors go away.

Remember this: if the console says there it is undefined, chances are that either you messed up in how to select the element OR it seriously doesn't exists because you misspelled a letter or let's say you forgot to add a class to a given tag (like in this case).

Third, your code in working condition is available below. It isn't using jQuery easing stuff but pure JavaScript - except by the onclick event that is better to use with jQuery:

$("#menu-dropdown").on("click", function() { 
 
  var menu_dropdown = document.getElementById("menudd");
 var menu_item = menu_dropdown.getElementsByTagName("A");
 
  $("#menudd").toggleClass("show");

 menu_dropdown.style.maxHeight = menu_item[0].offsetHeight * menu_item.length + "px";
  
  } ); 
#menudd {
  padding: 0.5em;
  position: fixed;
  overflow: hidden;
  max-height: 0px;
  opacity: 0;
  width: 200px;
  border-radius: 10px;
  box-shadow: 0 0 35px 15px black;
  transition: all 1s ease;
}

#menudd.show {
  opacity: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<span id="menu-dropdown">DropDown</span>
<div id="menudd">
  <a href="index.html">Home</a><br />
  <a href="aboutme.html">About Me</a><br />
  <a href="shotterm.html">Short-Term</a><br />
  <a href="middleterm.html">Middle-Term</a><br />
  <a href="longterm.html">Long-Term</a><br />
</div>

You can view it working also on this video (Youtube) that I recorded.

Fourth, you can add classes, in pure JavaScript, easily. I won't write about it in full since there is a famous question with a godly answer by Peter Boughton already on Stack Overflow. Read it here.

Anyway, to add a class, simply call:

document.getElementById("MyElementID").className += " MyClass";

It is important to note the space. Since it is a string with values, you need to put it otherwise it will treat all the classes as one word (one class) that doesn't exists.

To remove:

document.getElementById("MyElementID").className = document.getElementById("MyElementID").className.replace( /(?:^|\s)MyClass(?!\S)/g , '' );

Explanation available through Peter's answer. So read it there (important)!

Finally, this is out of the scope of the question and merely a reminder: I need to state that if this is a dropdown menu, you will need to add the show class after some focus (or click even) by calling a function in a master menu of some sort and not by hardcoding it into the <div> like I did to show the code working. For example, a "Cars Brand" item of a menu would have a dropdown with the available car brands (Ford, Toyota and etc) after focusing on it.

So, it is wise to transform it in a function that receives the ID of the target dropdown to open. And do not forget to close it after losing focus (or whatever).

José
  • 354
  • 5
  • 18
  • I have a button set up, and it works 100% fine. The reason i dont have class "show" on the div, is because i am toggeling it with this `$("#menubtn").on("click", function() { $("#menudd").toggleClass("show"); });` When i do what you suggested, I just set the max-height of the div when its even not visible, meaning the transition wont work – Lukas Knudsen Nov 10 '17 at 22:35
  • @LukasKnudsen is this (https://www.youtube.com/watch?v=iphowcIYqIM) the behavior you are expecting? If yes, look at the code again. I edited it. If not, I suggest being more clearer because technically, this all responds your question. So I'm not sure many people are following what is exactly the problem with your code or what you are trying to achieve. Like, show a foreign example of what you want and one of what you have (photos helps in these cases). – José Nov 11 '17 at 00:12
  • Your answer was perfectly correct. Your answer helped me. I got the final information on this artical: https://stackoverflow.com/questions/47232737/jquery-css-not-working/47232819#47232819 – Lukas Knudsen Nov 11 '17 at 00:14