Vince Bernabe Vince Bernabe - 7 months ago 22
Javascript Question

floating sidebar JQUERY

Im trying to hide the div on the second click of button 1. When i click button 1 on the first click, it shows the div, and on the second click it hides the div. The problem is, when i click to button 1 and then click to button 2, and suddenly i click to button 1 again, it hides the div. the first click of button 1 is still recorded when i clicked to button 2. I want to reset the number of clicks to button 1 so that when i click to button 2, and click to button 1, the div does not hide. please help sorry for my english



<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
</head>


<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css">

<style type="text/css">
.space{
border: 1px solid red;
width: 400px;
margin-left: -420px;
transition: 0.5s ease;
}
.show{
margin-left: 0;
}
.display{
display: none;
}
</style>

<body>
<div class="container-fluid">
<div class="buttons">
<div class="row">
<div class="col-md-3">
<button class="btn btn-primary" id="btn"><i class="fa fa-bars"></i></button>

<button class="btn btn-danger" id="btn2"><i class="fa fa-bars"></i></button>
</div>
</div>
</div>

<div class="space">
<div class="row">
<div class="col-md-9 divpage" id="div">
<h1>This is Div 1</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<div class="col-md-9 divpage2" id="div2">
<h1>This is Div 2</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
</div>
</div>
</div>





<script>
$(document).ready(function(){



$('#btn').click(function(){
var clicks = $(this).data('clicks');
if (clicks) {
$('.space').removeClass('show');
} else {
$('.space').addClass('show');
$('#div2').addClass('display');
$('#div').removeClass('display');
}
$(this).data("clicks", !clicks);
});

$('#btn2').click(function(){
$('.space').addClass('show');
$('#div').addClass('display');
$('#div2').removeClass('display');

});

$(document).mouseup(function (e)
{
var container = $('.space');

if (!container.is(e.target) // if the target of the click isn't the container...
&& container.has(e.target).length === 0) // ... nor a descendant of the container
{
container.removeClass('show');
}
});
});
</script>
</body>
</html>




Raj Raj
Answer
  $('#btn').click(function()
      {
          var divatr=$('#div2').attr('check');
          if(divatr=='unchecked')
          {
           $('#div2').attr('check','checked');
           $('#div2').show();
          }
          else
          {
            $('#div2').attr('check','unchecked');
            $('#div2').hide();

          }



      });

<div class="col-md-9 divpage2" id="div2" check="unchecked" style="display:none;">
                    <h1>This is Div 2</h1>
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
                    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
                    quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
                    consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
                    cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
                    proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
                    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
                    quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
                    consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
                    cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
                    proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
                </div>