Wrong position of OnClick Dropdown Menu - javascript

Hello, this is the link before click. And when I click on that link, that link go to upper side of the page, like this;
You can see the difference. I am also attaching the code please check.
Here is what I am working on;
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
// Close the dropdown if the user clicks outside of it
window.onclick = function(e) {
if (!e.target.matches('.dropbtn')) {
var myDropdown = document.getElementById("myDropdown");
if (myDropdown.classList.contains('show')) {
myDropdown.classList.remove('show');
}
}
}
.dropdown-content {
display: none;
position: relative;
width: 200px;
background-color: #FFF;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color: #000;
text-decoration: none;
display: block;
padding: 5px 10px;
font-size: 13px;
text-align: left;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.show {display: block;}
<div class="header">
<table border="0" width="100%">
<tr>
<td width="5%" valign="top"><img src="./images/logo_small.png" alt="" title=""></td>
<td width="45%"><div class="web_title">ASHNAB</div></td>
<td width="50%" align="right">
<div class="header_links">
Link Drop Down
</div>
<div id="myDropdown" class="dropdown-content">
Home
About
Contact
</div>
</td>
</tr>
</table>
</div>
I don't know where the issue lies.
Please help

It seems like a small thing, but there you go. Change only the CSS:
.dropdown-content {
display: none;
position: absolute;
right: 0;
top: 2rem;
width: 200px;
background-color: #FFF;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
The absolute positioning takes the element out of the normal flow. That's about it.

Related

How to make Custom CSS select list appear as a modal when the button is clicked?

I have created a custom CSS select and need help to make the #drop in form of a modal.
So that whenever a user clicks on the dropdown button, it will open a modal containing a list of options. And selecting a list will show information on that particular list option.
Also, the #drop div does not appear in the middle of the screen.
I'm new to JS and have managed to create a structure. I'm sharing the code below. Please help me with the modal so that the entire thing works perfectly.
Thanks in advance.
PS: If you think there's some easy way of doing this please add the code to the answer.
<!-- HTML -->
<div class="dropdown">
<button onclick="toggleDrop(document.querySelector('#drop'))" class="dropbtn"></button>
<div id="drop" class="dropdown-content d-none">
<ul>
<li>Click for Bird Name</li>
<li>Click for Animal Name</li>
</ul>
</div>
</div>
<div class="tabs">
<!-- Bird Name -->
<div tab-name="dl-tabOne" class="dl-tabcontent">
Parrot
</div>
<!-- Animal Name -->
<div tab-name="dl-tabTwo" class="dl-tabcontent d-none">
Lion
</div>
</div>
<!-- CSS -->
.d-none {
display: none;
}
.dropbtn {
display: block;
font-size: 40px;
font-weight: 700;
margin: auto;
width: auto;
outline: none;
border: none;
border-bottom: 2px solid ;
border-bottom-style: dotted;
height: 60px;
cursor: pointer;
overflow: hidden;
color: #150ee6;
background: none;
}
.dropdown {
position: relative;
margin: 30px 30px 10px;
text-align: center;
}
.dropdown-content {
position: absolute;
background-color: #fff;
min-width: 250px;
box-shadow: 0 4px 12px 0 rgba(0, 0, 0, 0.1), 0 5px 4px -5px rgba(0, 0, 0, 0.1);
z-index: 1;
text-align: left;
padding: 20px;
}
.dropdown-content ul {
padding: 0;
}
.dropdown-content li {
list-style-type: none;
border-bottom: 1px solid #dfe1e5;
}
.dropdown-content li:last-child{
border: none;
}
.dropdown-content a {
color: #0d0d21;
padding: 15px 10px;
font-size: 1rem;
text-decoration: none;
list-style-type: none;
display: block;
font-weight: 600;
}
.dropdown-content a:hover {
background-color: #f2f2f2;
}
.tabs {
max-width: 720px;
margin: 0 auto;
}
.dl-tab-abt{
margin-top: 25px !important;
}
<!-- JS -->
setButtonInnerText(document.querySelector(".dropbtn"))
function openPage(id, a) {
const p = document.querySelector("[tab-name='" + id + "']");
const tabs = document.querySelectorAll(".tabs .dl-tabcontent");
for (var x = 0; x < tabs.length; x++)
tabs[x].classList.add("d-none");
if (p !== null) {
document.querySelector(".dropbtn").innerHTML = a.innerHTML;
p.classList.remove("d-none");
}
}
function toggleDrop(drop) {
drop.classList.toggle("d-none");
drop.onclick = function (e) {
drop.classList.add("d-none");
}
}
function setButtonInnerText(btn) {
const firstOption = btn.parentElement.querySelector("#drop a:first-child");
btn.innerHTML = firstOption.innerHTML;
}

Click for Dropdown not working when generated in a loop

I have a table that is populated from a database with a dropdown menu that is populated along side the table which is generated from code in the php. Im tying to use javascript and css to make each drop down open on click but I cannot seem to get it to work. I've included the code below. I initilly had the script working but it only worked on the first instance of the dropdown. I reviewed a similar post to what Im looking to do but it was to no avail.
CSS
background-color: #04AA6D;
color: white;
padding: 5px;
font-size: 12px;
border: none;
text-transform: uppercase;
}
.yardDropbtn i{
font-size: 12px;
top: 1px;
position: relative;
}
.yardDropdown{
position: relative;
display: inline-block;
}
.yard-dropdown-content{
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
right: 0;
}
.yard-dropdown-content a{
color: black;
padding: 5px 16px;
text-decoration: none;
display: block;
}
.yard-dropdown-content a:hover {
background-color: #ddd;
}
.yardDropBtnCt {
display: none;
background-color: #f1f1f1;
color: black;
/*margin-left: 5px;*/
border: 1px;
display: block;
width: 100%;
height: 30px;
font-size: 15px;
text-transform: uppercase;
font-size: 12px;
}
.yardDropBtnCt:hover {
background-color: #ddd;
}
.trackBtn{
display: none;
background-color: #f1f1f1;
color: black;
margin-left: 5px;
border: 1px;
display: block;
width: 100%;
height: 30px;
font-size: 15px;
}
.trackBtn:hover{
background-color: #ddd;
}
/*.yard-dropdown-content:hover{
background-color: #ddd;
}*/
.show_yard_Dropdown .yard-dropdown-content {
display: block;
}
.show_yard_Dropdown .yardDropbtn {
background-color: #3e8e41;
}
JavaScript
var dropDownDivY = document.querySelector(".yardDropdown");
/*dropDownButton.addEventListener("click", function(){
dropDownDivY.classList.toggle('show_yard_Dropdown');
});*/
dropDownButton.forEach(btn => {
btn.onclick = function() {
dropDownDivY.style.display = "block";
}
});
HTML
<tr>
<td width='50px' class='yard8'>6400'</td>
<td width='100px' class='yard8 track'>Receiving 1</td>
<td width='150px' class='yard8'></td>
<td width='150px' class='yard8'>
<td width='250px' class='yard8 destination'></td>
<td width='100px'class='yard8 length'>3455'</td>
<td width='100px' class='yard8 weight'></td>
<td width='150px' class='yard8 status'></td>
<td class='yard8'>
<div class='progressContainer'>
<div class='progress' style='width:54%'>54%</div>
</div>
</td>
<td class='yard8 remarks'></td>
<td width='59px'>
<div class="yardDropdown">
<button class="yardDropbtn">Menu<i class="bx bx-menu"></i></button>
<div class="yard-dropdown-content">Edit TrackAEI Populate</div>
</div>
</td>
</tr>
<form action="" method="POST"></form>
document.querySelector(".yardDropdown");
The querySelector method only returns the first element matching the selector, so it can never work for more than the first row.
You need to use querySelectorAll instead and then loop over all the elements found:
let dropDowns = document.querySelectorAll(".yardDropdown");
for (let i = 0; i < dropDowns.length; i++) {
let dropDown = dropDowns[i]
dropDown.addEventListener('click', function () {
// do something, you can use the `i` variable to know which row has been clicked if you need to
})
}

Hover command stops working when hiding nested div

I am trying to use the hover command in CSS and it works when hovering over my property button, but I want it to hide after the click event in JavaScript.
Below is an example of my code with a "property" button and then a dropdown that comes when you hover over it. If I add $(".dropdown-content").css("display","none"); to the button click event then the hover command stops working.
I don't understand why this is the case as I would be hiding a nested div .dropdown-content and so the hover command should still work for the outer div `.dropdown but it doesn't. Any help would be appreciated!
$("#001").on('click', function() {
window.whichtab = 1;
});
$("#002").on('click', function() {
window.whichtab = 2;
});
$("#001,#002").on('click', function() {
$("#title1,#title2").hide()
if (window.whichtab == 1) { //home
$("#title1").show();
$("#title1").css("display", "inline");
//$(".dropdown-content").css("display","none");
}
if (window.whichtab == 2) {
$("#title2").show();
$("#title2").css("display", "inline");
//$(".dropdown-content").css("display","none");
}
});
.dropbtn {
background-color: #3d70b2;
color: white;
padding: 13px;
font-size: 16px;
border: none;
}
.dropdown-content button {
color: black;
border: none;
width: 250px;
height: auto;
padding: 13px 16px;
text-align: left;
text-decoration: none;
display: inline-block;
cursor: pointer;
}
.dropdown-content {
display: none;
border: solid;
border-radius: 0px;
border-color: #3d70b2;
position: absolute;
background-color: 17, 83, 238;
min-width: 250px;
box-shadow: 0px 12px 16px 0px rgba(0, 0, 0, 0);
z-index: 1;
}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown:hover .dropbtn {
background-color: #3d70b2;
}
#title1 {
display: inline;
}
#title2 {
/*defult title is hidden for 2*/
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="dropdown">
<!--title of each tab-->
<button class="dropbtn">Property</button>
<h4 id="title1">   Home page</h4>
<h4 id="title2">   Content Page</h4>
<div class="dropdown-content">
<!--each button going to each tab-->
<button id="001" ,class='noHover'>Home Page</button><br>
<button id="002">Content Page</button><br>
</div>
</div>
display: none doesn't preserve the space and you can not hover on that. You can use opacity property like the following way:
$(".dropdown-content").css("opacity", 0); // hide
$(".dropdown-content").css("opacity", 1); // show

Menubar dropdown context

/* 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 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.remove('show');
}
}
}
}
body, html {
height: 100%;
}
.parallax {
background-image: url('../images/firstpage.jpg');
height: 100%;
margin: 0;
/* Create the parallax scrolling effect */
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
button{
background:linear-gradient(to right, rgba(255,0,0,0), rgba(255,255,255,0));
border: none;
font-family: "Roboto";
color: rgba(0, 0, 0, 0.5);
text-shadow: 1px 1px 1px rgba(255, 255, 255, 0.4);
text-decoration: none;
display: block;
display: inline-block;
font-size: 26px;
z-index: 1;
float: left;
}
.fixed{
position: fixed;
}
.textbackground
{
position: absolute;
left: 100px;
top: 150px;
}
.textbackgroundbar{
overflow: hidden;
width: 800px;
height: 50px;
background: linear-gradient(to right,rgba(255,255,255,30), rgba(255,0,0,0), rgba(255,255,255,30));
}
.dropbtn {
display: block;
color: black;
padding: 10px;
font-size: 24px;
border: none;
cursor: pointer;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background: linear-gradient(rgba(0,0,0,0.3),rgba(0,0,0,0.1));
min-width: 800px;
min-height: 700px;
overflow: auto;
z-index: 1;
}
.dropdown-content a {
color: red;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.show {display:block;}
.dropdown-content1 {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown:hover .dropdown-content1 {
display: block;
}
<div class="parallax">
<div class="textbackground">
<div class="textbackgroundbar">
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
<button onclick="myFunction()" class="dropbtn">Dropdown2</button>
</div>
</div>
<div id="myDropdown" class="dropdown-content">
Home
About
Contact
</div>
<div id="myDropdown" class="dropdown-content">
Home2
About2
Contact2
</div>
</div>
</div>
What is the problem here? It should dropdown 2 different things when i click the specific button .. but it doesn't.
If i click dropdown it shows home about contact. If i click dropdown 2, same home about contact, but i want it to be home2 about2 contact2.
The entire website contains 5 parallax slides. It is a project for school, to obtain some sort of license, so i'm really trying to understand that code ( and mostly i do ). I understand every "function" but when i combine them all... i just screw up.
So, please, be as clear as you can. Thank you !
P.S: Excuse my bad english, it's not my first language.. :(
You are using the same ID for two different div elements.
IDs must be unique.
You may solve this issue simply adding the id text and event object to your inline events:
<button onclick="myFunction('myDropdown1', event)" class="dropbtn">Dropdown</button>
<button onclick="myFunction('myDropdown2', event)" class="dropbtn">Dropdown2</button>
And so you can change your function in order to handle the parameters.
When clicking on a dropbtn button:
stop event propagation in order to avoid the window.onclick execution
hide/remove show class to the dropdown-content div if any
toggle the show class on the current dropdown-content div
When clicking on the window be sure to be outside the dropdown-content visible div and dropbtn buttons.
For more details take a look to:
var elt = element.closest(selectors);:
The Element.closest() method returns the closest ancestor of the current element (or the current element itself) which matches the selectors given in parameter. If there isn't such an ancestor, it returns null.
- >var result = element.matches(selectorString);
The snippet:
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction(eleId, event) {
//
// stop event propagation in order to avoid the window.onclick execution
//
event.stopPropagation();
//
// remove show class to previous shown div
//
document.querySelectorAll('.dropdown-content.show').forEach(function(ele, idx) {
ele.classList.remove("show");
});
//
// select by id using the param value
//
document.getElementById(eleId).classList.toggle("show");
}
// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
//
// if not a button and not a dropdown-content....
//
if (!event.target.matches('.dropbtn') && event.target.closest('.dropdown-content') == null) {
//
// remove show class to previous shown div
//
document.querySelectorAll('.dropdown-content.show').forEach(function(ele, idx) {
ele.classList.remove("show");
});
}
}
button{
background:linear-gradient(to right, rgba(255,0,0,0), rgba(255,255,255,0));
border: none;
font-family: "Roboto";
color: rgba(0, 0, 0, 0.5);
text-shadow: 1px 1px 1px rgba(255, 255, 255, 0.4);
text-decoration: none;
display: block;
display: inline-block;
font-size: 26px;
z-index: 1;
float: left;
}
.fixed{
position: fixed;
}
.textbackground {
position: absolute;
left: 100px;
top: 150px;
}
.textbackgroundbar{
overflow: hidden;
width: 800px;
height: 50px;
background: linear-gradient(to right,rgba(255,255,255,30), rgba(255,0,0,0), rgba(255,255,255,30));
}
.dropbtn {
display: block;
color: black;
padding: 10px;
font-size: 24px;
border: none;
cursor: pointer;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background: linear-gradient(rgba(0,0,0,0.3),rgba(0,0,0,0.1));
min-width: 800px;
min-height: 700px;
overflow: auto;
z-index: 1;
}
.dropdown-content a {
color: red;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.show {display:block;}
.dropdown-content1 {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown:hover .dropdown-content1 {
display: block;
}
<div class="parallax">
<div class="textbackground">
<div class="textbackgroundbar">
<div class="dropdown">
<button onclick="myFunction('myDropdown1', event)" class="dropbtn">Dropdown</button>
<button onclick="myFunction('myDropdown2', event)" class="dropbtn">Dropdown2</button>
</div>
</div>
<div id="myDropdown1" class="dropdown-content">
Home
About
Contact
</div>
<div id="myDropdown2" class="dropdown-content">
Home2
About2
Contact2
</div>
</div>
</div>

Opening a popup from a dowpdown menu

I want to be able to open a drop down menu, and when I select one of the options, a small window will popup with more options available. Something shown in this picture I found: http://datasmugglers.com/wp-content/uploads/mute-someone.jpg
I tried some solutions I found but no matter what I do it doesn't seem to work. I can't find any errors but I'm trying to do it exactly like the template someone gave me in my last question and its just not working.
//toggles the hidden dropdown
function dropdown() {
document.getElementById("myDropdown").classList.toggle('show');
}
//opens a popup window from clicking settings
function settings() {
document.getElementById('settingsD').classList.toggle('show');
}
/* dropdown button*/
.dropbtn {
background-color:#4caf50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
/*drop down button on hover & focus*/
.dropbtn:hover, .dropbtn:focus {
background-color: #3e8e41;
}
/*the container <div> needed to postision the content */
.dropdown {
position: relative;
display: inline-block;
}
/*dropdown content*/
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
/*changes the style of the list*/
.dropdown-content2 {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/*links in the dropdown*/
.dropdown-content a:hover {background-color: #f1f1f1}
/*show the dropdown (use Js to add this class to the dropwdown-content container when its clicked)*/
.show {display:block;}
.settings {
display: none;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
z-index: 10000;
max-width: 26em;
height: 199px;
padding: 28px 22px;
border: 4px solid rgb(197, 218, 255);
background: black;
}
<!DOCTYPE html>
<html>
<link type="text/css" rel="stylesheet" href="dropdown.css">
<script src="dropdown.js" type="text/javascript"></script>
<head>
<div class="dropdown">
<button onclick='dropdown()' class="dropbtn">Dropdown</button>
<div id="myDropdown" class="dropdown-content">
<div class='dropdown-content2' onclick="settings()">Settings</div>
<span id='settingsD' class='settings'>These are the settings</span>
<div class='dropdown-content2'>Link 2</div>
<div class='dropdown-content2'>Link 3</div>
</div>
</div>
</head>
</html>
As you can see I can't click on the settings part of the dropdown and have the settings text show, and I'm not sure why. Thank you for your help in advance!
You have to place the .show below .settings in your css. If you have it like you do the display:block; in .show gets overwritten by the display:none; in .settings which is below.
Else you could do .settings:not(.show) if none of the css for .settings should be displayed.

Categories