Open drop down under its own button

hi all

i want to open
2nd dropdown under 2nd button
3 dropdown under 3rd button

how to shift dropdowns under their own buttons

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>

<style type="text/css">
 /* Dropdown Button */
 body{margin:50px;}
a.bt {
    background-color: #f00;
    color: #fff;
    padding: 16px;
    font-size: 16px;
    cursor: pointer;
    z-index:2;
    position:relative;
    display:inline-block;
    margin-right:25px;
    font-family:Arial, Helvetica, sans-serif
}
.dpactive{
    background-color: #fff;
    color: #666;
    padding: 16px;
    font-size: 16px;
    cursor: pointer;
    z-index:2;
    position:relative;
    display:inline-block;
    background-color: #fff;
    border:1px solid #CCCCCC; 
    border-bottom:0px;
    display:inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    min-width: 160px;
    border:1px solid #CCCCCC; 
    margin-top:-1px;
}
.dropdown-content a{
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    outline:0;
}
.dropdown-content a:hover {background-color: #f1f1f1;}
.show {display:block;}
</style>

</head>
<body>
  <a class="bt">Button 1</a>
  <div class="dropdown-content">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
  <a class="bt">Button 2</a>
  <div class="dropdown-content">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
    <a class="bt">Button 3</a>
  <div class="dropdown-content">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script type="text/javascript">
     $('.bt').click( function(e) {
        e.preventDefault();
        e.stopPropagation(); 
        $('.dropdown-content').show();
        $(this).addClass('dpactive');

    });
    
    $('.dropdown-content').click( function(e) {
        e.stopPropagation(); 
        $('.dropbtn').show();
    });
    
    $('html').click( function() {
        $('.dropdown-content').hide();
        $('.bt').removeClass('dpactive');
        });  
    
</script>
  </body>
</html>

vineet

You could group those dropdowns in separate wrapper-divs so that the content gets aligned to its button automatically, like

HTML

<div class="dropdown-wrap">
  <a class="bt">Button 1</a>
  <div class="dropdown-content">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
</div>

<div class="dropdown-wrap">
  <a class="bt">Button 2</a>
  <div class="dropdown-content">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
</div>

<div class="dropdown-wrap">
  <a class="bt">Button 3</a>
  <div class="dropdown-content">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
</div>

CSS

.dropdown-wrap {
  display: inline-block;
  position: relative;
}

To show only the dropdown associated with a button, you’ll have to select it accordingly. One possibilty would be e.g.

// Store a reference to the dropdowns so that
// you don't have to query the DOM anew each time
var dropdowns = $('.dropdown-content');

$('.bt').click(function(e) {
   e.preventDefault();
   e.stopPropagation();

   dropdowns.hide();

   // Only show the dropdown being the direct next sibling
   // of the button clicked
   $(this).next(dropdowns).show();
   $(this).addClass('dpactive');
});

$(document).click(function() {
   dropdowns.hide();
   $('.bt').removeClass('dpactive');
});

Thanks m3g4p0p

its working perfect now

vineet

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.