Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Return" content when closing toggle

Tags:

javascript

The goal is to hide the #boxar when the toggle is active and return the "#boxar" when "toggle is closed. The code works fine until I close the toggle (the "#boxar" disappears) but when I close the toggle, they won't return.

Anyone who knows how to fix this?

$(document).ready(function(){
    $('#toggle').click(function(){
        $('.boxar').hide();
        $('#'+this.rel+'').show();

        return false;
    });
});
like image 719
Rebecca Seiron Avatar asked Jul 12 '18 11:07

Rebecca Seiron


1 Answers

You need to use toggle() instead, like :

$(document).ready(function(){
    $('#toggle').click(function(){
        $('.boxar').toggle();
        $('#'+this.rel+'').toggle();

        return false;
    });
});

$(document).ready(function() {
  $('#toggle').click(function() {
    $('.boxar').toggle();
    $('#' + $(this).attr('rel')).toggle();

    return false;
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button type='button' id='toggle' rel='test'>Toggle</button>
<br>
<div class='boxar'>Boxar DIV</div>

<span>Regular span</span><br>
<span id="test">Rel span</span><br>
<span>Regular span</span><br>
<span>Regular span</span>
like image 54
Zakaria Acharki Avatar answered Oct 20 '22 23:10

Zakaria Acharki