-2

I just created my fist dropdown, so far so good. the dropdown opens. Below the dropdown button is text and images in a container. When i click the dropdown button, the list is behind the text and images that is in the container. How do I bring the dropdown list to the foreground?

<td bgcolor="white" height="50">
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn"><FONT face="Verdana"    color=#026991 size=2><b>Onze diensten</b></button>
<div id="myDropdown" class="dropdown-content">
<a href="#"><b>Link 1</b></a>
<a href="#"><b>Link 2</b></a>
<a href="#"><b>Link 3</b></a>
<a href="#"><b>Link 4</b></a>
<a href="#"><b>Link 5</b></a>

</div>
</div>




 <script>
/* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {

if (!event.target.matches('.dropbtn')) {

var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
  var openDropdown = dropdowns[i];
  if (openDropdown.classList.contains('show')) {
    openDropdown.classList.contains('show');
  }
  }
  }
  }
  </script>

CSS:

/* Dropdown Button */
.dropbtn {
background-color: white;
color: #026991;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}

/* Dropdown button on hover & focus */
.dropbtn:hover, .dropbtn:focus {
background-color: white;
border: none;
}

/* The container <div> - needed to position the dropdown content */
.dropdown {
position: relative;
display: inline-block;
}

/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background-color: white;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

/* Links inside the dropdown */
.dropdown-content a {
color: #026991;
padding: 12px 16px;
text-decoration: none;
display: block;

}

/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: white}

/* Show the dropdown menu (use JS to add this class to the .dropdown-content          container when the user clicks on the dropdown button) */
.show {display:block;}
henkg
  • 1
  • 3

3 Answers3

0

Try changing your HTML like this. `

<td bgcolor="white" height="50">
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn"><FONT face="Verdana"    color=#026991 size=2><b>Onze diensten</b></button>
<div id="myDropdown" class="dropdown-content">
<select>
<option href="#"><b>Link 1</b></option>
<option href="#"><b>Link 2</b></option>
<option href="#"><b>Link 3</b></option>
<option href="#"><b>Link 4</b></option>
<option href="#"><b>Link 5</b></option>
</select>
</div>
</div>




 <script>
/* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {

if (!event.target.matches('.dropbtn')) {

var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
  var openDropdown = dropdowns[i];
  if (openDropdown.classList.contains('show')) {
    openDropdown.classList.contains('show');
  }
  }
  }
  }
  </script>

` And also note that in the code you have posted here. You haven't mentioned where your CSS is. you can add that by

  <link rel="stylesheet" href="styles.css">

.

  • The dropdown is now in the foreground, but...when i press the dropdown button, only Link1 shows. I must press Link 1, to show the other lines. I wish to see all lines – henkg Dec 04 '16 at 07:43
  • Sorry, but i can't manage to get it right, this is so new for me. The problems I have: Problem 1. If I press the button, only Link 1 shows. I want all Links to show when I press the button. Problem 2: if I choose one of the links, the selected webpage must be opened, but nothing happens. This is what I did: Sorry for I am a newbie and learning...... – henkg Dec 04 '16 at 15:33
  • So what you want is just show those 5 links when that button is pressed? am I correct? – Dimuth Tharaka Menikgama Dec 05 '16 at 05:28
  • I want 2 things: 1. When the button is pressed, i want to see the 5 links (i will add a few more in future). 2. Choose one of the 5 links, the website must show that html page of my website. – henkg Dec 05 '16 at 10:08
  • So you don't need a drop-down? you can just add a div that can show/hide based on a button click. correct me if I'm wrong? If so I can fix this code for you – Dimuth Tharaka Menikgama Dec 06 '16 at 03:53
  • this is what I want: Pressing button: Drop down shows all items in foreground (what i was building, the drop down opens, but the items are behind the text in
    ). When selecting one of the items e.g. Home Henk, that page must open. Thanks for your help! I will send you a personal message on FB
    – henkg Dec 06 '16 at 07:13
0

Ok. After reading your comments I understood what are your requirements clearly. Then I did a little bit research on "Expand dropdown programmatically in html" . Then I read following stackoverflow question and answersQ1 , Q2. Most of the contributors to those answers have mentioned that there is no way to do that in HTML. Provided code examples were also not working. (Complaints can be seen on comments also). Then I came up with the method below to fulfill your two requirements. Though it uses buttons to show dropdown , it works as a normal HTML dropdown. The only problem is that you have to keep enough space below the button so that Expansion does not affect other html elements in the design.

<!DOCTYPE html>
<html>
<head>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script>
        $(document).ready(function(){



                $("#btn1").click(function(){
                    var val=document.getElementById("btn1").value;

                    if(val=="0"){
                        document.getElementById("btn1").value=1;
                        a

                        jQuery('#content').toggle('show');

                    }
                    else{
                        document.getElementById("btn1").value=0;


                        jQuery('#content').toggle('hide');

                        document.getElementById("btn1").value=0;
                    }

                });





        });
    </script>
</head>
<body>


<div name="drop-down">
<button id="btn1" value="1">Show/Hide</button>
    <div id="content" hidden>
        <button    id="btn11" value="0">Link1</button>
        </br>
        <button   id="btn12" value="0">Link2</button>
        </br>
        <button  id="btn13" value="0">Link3</button>
        </br>
        <button    id="btn14" value="0">Link4</button>
        </br>
    </div>

</div>



<p id="demo"></p>



</body>
</html>
Community
  • 1
  • 1
  • I really appreciate all you help...... i added your lines. But the result is not good, The result you can see here: http://www.flex-experts.nl/beta/ The drowndown opens behind the text below and not in foreground. Maybe what I want is impossible... Other issue is, that I still not know how to use the Link1 etc to open another page of the website. – henkg Dec 07 '16 at 09:36
0

I have a solution for you that is in CSS3.

.dropdown-content {
display: none;
position: absolute;
background-color: white;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 4;
}

"z-index" is basically what layer that one thing is.

Leo Correa
  • 19,131
  • 2
  • 53
  • 71
Ben
  • 57
  • 9