Click here to Skip to main content
15,907,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
What I am trying to achieve is to close one DIV when other DIV is opened (click open) with JQUERY. I am a beginner in JAVASCRIPT/JQUERY. Following is the HTML structure.

I am also interested to know if, in this case multiple DOM elements can be controlled with a single function.

HTML Structure









Here on clicking the class .region-search and its children, I could get the region-dropdown to slide down. I could also get the company registration model to click open as well. However, when the first DIV is active, when I click the second DIV, the first one remains active. I need to close it. How can I do it?. Please assist me.

I am also trying to use the same JQUERY function to control both DOM. Is it possible? Please advise.

The JQUERY is as follows

$(document).ready(function(){

var $registration_dropdown=$('.company-registration-modal');
var $registration_link=$('.top-register span, .fa-registered');

$registration_dropdown.hide();
$registration_link.on('click touchend', function(event){

var target = $(event.target);

if(target.is($registration_link)){
$registration_dropdown.slideToggle(300);
event.stopPropagation();
}
});

$(document).click(function(e) {
if ($(e.target).closest($registration_dropdown).length===0) {
$($registration_dropdown).slideUp(300);
}
});

$registration_dropdown.mouseleave(function(){
$(this).delay(400).slideUp(300);
});

});


Similarly, I write a different function for the second DIV.

What I have tried:

$(document).ready(function(){

var $registration_dropdown=$('.company-registration-modal');
var $registration_link=$('.top-register span, .fa-registered');

$registration_dropdown.hide();
$registration_link.on('click touchend', function(event){

var target = $(event.target);

if(target.is($registration_link)){
$registration_dropdown.slideToggle(300);
event.stopPropagation();
}
});

$(document).click(function(e) {
if ($(e.target).closest($registration_dropdown).length===0) {
$($registration_dropdown).slideUp(300);
}
});

$registration_dropdown.mouseleave(function(){
$(this).delay(400).slideUp(300);
});

});
Posted
Updated 26-Dec-16 19:04pm
Comments
baotdinh 12-Nov-16 2:16am    
post your html too

1 solution

I think you want to show only current clicked div and rest of other should be hide .. i have given you an example below can help you to implement..

HTML
<div id="dropdown-1" class="dropdown dropdown-processed">
  <a class="dropdown-link" href="#">Options</a>
  <div class="dropdown-container" style="display: none;">
    <ul>
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3</li>
    </ul>
  </div>
</div>

<div id="dropdown-2" class="dropdown dropdown-processed">
  <a class="dropdown-link" href="#">Options</a>
  <div class="dropdown-container" style="display: none;">
    <ul>
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3</li>
    </ul>
  </div>
</div>

<div id="dropdown-3" class="dropdown dropdown-processed">
  <a class="dropdown-link" href="#">Options</a>
  <div class="dropdown-container" style="display: none;">
    <ul>
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3</li>
    </ul>
  </div>
</div>

JQuery single function as below :
JavaScript
$(document).ready(function(){

  $('div.dropdown').each(function() {
    var $dropdown = $(this);

    $("a.dropdown-link", $dropdown).click(function(e) {
      e.preventDefault();
      $div = $("div.dropdown-container", $dropdown);
      $div.toggle();
      $("div.dropdown-container").not($div).hide();
      return false;
    });

});
    
  $('html').click(function(){
    $("div.dropdown-container").hide();
  });
     
});
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900