-4

I want to open and close my menu with same button. here is my code.

<script>
$('#open-menu').click(function() {
  $("#mySidenav").css("width" , "250px");
  $("#l_m").css({"marginLeft": "250px", "position" : "absolute", "width" : "100%"});
  // $(this).attr('id','close-menu');
});

$('#close-menu').click(function() {
  $("#mySidenav").css("width" , "0");
  $("#l_m").css("marginLeft" , "0");
});
</script>

Check this fiddle : https://jsfiddle.net/uyjogg5e/10/

(I tried to add the line that is commented but when I click on it, nothing happens)

Abhishek Pandey
  • 13,302
  • 8
  • 38
  • 68
Rocstar
  • 1,427
  • 3
  • 23
  • 41

2 Answers2

1

You can set the css property in a class:

.side-nav-show{
   width : 250px;
}
.l_m-show{
   marginLeft:250px;
   position:absolute;
   width:100%;
}

and then toggle the classes on click of element:

$('#open-menu').click(function() {
  $("#mySidenav").toggleClass('side-nav-show');
  $("#l_m").toggleClass('l_m-show');
});

Working Demo

Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
1
var a = 0;
$('#open-menu').click(function() {
    if(a % 2 == 0){
       $("#mySidenav").css("width" , "250px");
       $("#l_m").css({"marginLeft": "250px", "position": "absolute", "width" : "100%"});
    }
    else{
       $("#mySidenav").css("width" , "0");
       $("#l_m").css("marginLeft" , "0");
    }
    a++;
});
Tomer Wolberg
  • 1,256
  • 10
  • 22