1

I wrote a toggle burger menu. I want to tell this code to hide <div class="line1"></div> and <div class="line3"></div> when the .menu is clicked.

How can I do this?

$(function(){

  $('.menu').click(function(){
    $('.sidebar').toggle();
  });

});
.menu {
background:#ddd;
padding:15px;
width:fit-content;
height:fit-content;
  position:relative;
}

.menu > div {
  background-color:black;
  width:22px;
  height:1px;
}

.line1 {
  top:12px;
  position:absolute;
}
.line2 {
  top:16px;
}
.line3 {
  top:22px;
}
.sidebar {
display:none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <div class="menu">
        <div class="line1"></div>
        <div class="line2"></div>
        <div class="line3"></div>
    </div>
  
  
  
    <div class="sidebar">
        Sidebar
    </div>

1

2 Answers 2

1

If you want to make it hide and then show, use .toggle(), like this:

$(function(){

  $('.menu').click(function(){
    $('.sidebar').toggle();
    $(".line1").toggle();
    $(".line3").toggle();
  });

});
.menu {
background:#ddd;
padding:15px;
width:fit-content;
height:fit-content;
  position:relative;
}

.menu > div {
  background-color:black;
  width:22px;
  height:1px;
}

.line1 {
  top:12px;
  position:absolute;
}
.line2 {
  top:16px;
}
.line3 {
  top:22px;
}
.sidebar {
display:none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <div class="menu">
        <div class="line1"></div>
        <div class="line2"></div>
        <div class="line3"></div>
    </div>
  
  
  
    <div class="sidebar">
        Sidebar
    </div>

Sign up to request clarification or add additional context in comments.

Comments

1

Use jQuery .hide()

If you want to show it again, use .show() the same way you used .hide()

Go to the following links to learn more

Hide: http://api.jquery.com/hide/

Show: http://api.jquery.com/show/

I updated your snippet below:

$(function(){

  $('.menu').click(function(){
    $('.sidebar').toggle();
    $(".line1").hide();
    $(".line3").hide();
  });

});
.menu {
background:#ddd;
padding:15px;
width:fit-content;
height:fit-content;
  position:relative;
}

.menu > div {
  background-color:black;
  width:22px;
  height:1px;
}

.line1 {
  top:12px;
  position:absolute;
}
.line2 {
  top:16px;
}
.line3 {
  top:22px;
}
.sidebar {
display:none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <div class="menu">
        <div class="line1"></div>
        <div class="line2"></div>
        <div class="line3"></div>
    </div>
  
  
  
    <div class="sidebar">
        Sidebar
    </div>

Or you can use .toggle()

http://api.jquery.com/toggle/

$(function(){

  $('.menu').click(function(){
    $('.sidebar').toggle();
    $(".line1").toggle();
    $(".line3").toggle();
  });

});
.menu {
background:#ddd;
padding:15px;
width:fit-content;
height:fit-content;
  position:relative;
}

.menu > div {
  background-color:black;
  width:22px;
  height:1px;
}

.line1 {
  top:12px;
  position:absolute;
}
.line2 {
  top:16px;
}
.line3 {
  top:22px;
}
.sidebar {
display:none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <div class="menu">
        <div class="line1"></div>
        <div class="line2"></div>
        <div class="line3"></div>
    </div>
  
  
  
    <div class="sidebar">
        Sidebar
    </div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.