Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8281041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:53:52+00:00 2026-06-08T09:53:52+00:00

Javascript code: $(.div1).click(function(event){ event.stopPropagation(); $(.div1 div).slideToggle(); }); $(.div2).click(function(event){ event.stopPropagation (); $(.div2 div).slideToggle(); }); $(document).click(function(event){

  • 0

Javascript code:

$(".div1").click(function(event){
    event.stopPropagation();
    $(".div1 div").slideToggle();
});

$(".div2").click(function(event){
    event.stopPropagation ();
    $(".div2 div").slideToggle();
});

$(document).click(function(event){
    $(".div1 div,.div2 div").slideUp();
});

Suppose I click .div1 & .div1 div slides down. Then i click .div2 But as event.stopPropagation is executed, .div1 div doesnot slide up. So what should i do? I dont want to put this code $(".div1 div").slideUp(); as it makes no sense when there’ll be too many divs like this

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T09:53:54+00:00Added an answer on June 8, 2026 at 9:53 am

    Never use Event.stopPropagation(). All the layers of any application should always, at any point, be able to register an event happened.

    Instead use the event.target and traverse with .closest() to achieve the same.

    The following will handle infinite numbers of DIVs, so you don’t need to rewrite your code. (PS: just add a .box to your parent elements to make all simpler)

    $(".box").on("click", function(event){
      const $div = $(this).find('div');
      $('.box > div').not($div).slideUp();
      $div.slideToggle();
    });
    
    $(document).on("click", function(event){
      if (!$(event.target).closest(".box").length) {
        $(".box > div:visible").slideUp();
      }
    });
    /*QuickReset*/ * {margin:0; box-sizing:border-box;}
    
    .box{
      background:#eee;
      width:160px;
      padding:10px 0;
      margin:10px;
    }
    .box h2{
      cursor:pointer;
    }
    .box div{
      display:none;
      background:#aaa;
      color:#cf5;
    }
    Click on 'body' to close opened DIVs
      
    <div class="div1 box">
      <h2>DIV 1</h2>
      <div>Lorem <br>Ipsum<br>....</div>
    </div>
      
    <div class="div2 box">
      <h2>DIV 2</h2>
      <div>Lorem <br>Ipsum<br>....</div>
    </div>
      
    <div class="div3 box">
      <h2>DIV 3</h2>
      <div>Lorem <br>Ipsum<br>....</div>
    </div>
    
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My JavaScript code is as follows: $(document).keydown(function(event){ var move, inter; clearInterval(inter); inter = setInterval(move
Javascript Code YAHOO.util.Event.onDOMReady(function (ev) { var carousel = new YAHOO.widget.Carousel(container, {isCircular: true, numVisible:1}); console.log(carousel.set('navigation',
I've a simple code as following: <html> <body> <div id=div1> <input class=input1 type=text value=click
I found this code <body> <div id=div1> What is 2+2? </div> <div id=div2> <a
I have the following code: <script type=text/javascript> $(function() { //div1 toggle function runEffect(){ var
javascript code: function getCheckbox(name,id){ var check = 0; var deger = option-checkbox-+id; var dom
This Javascript code is using the 'this' keyword inside a nested function (which is
My javascript code is like this: function openWindows() { if(A is true) { window.open(URL_A);
Is it possible to somehow shorten this code: var i=GetStringFromServer('/url'); if(i){ $('#Div1').hide(); $('#Div2').show(); }
the body html code: <div style=background:yellow;width:500px;height:300px;> <div id=div1 style=background:red; width:100px;height:100px; float:left;></div> <div id=div2 style=background:blue;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.