0

How can I hide a toggle when there's another toggle and if the user show the one inside and you hide the one outside the one inside keeps there? It should be hidden too.

I was not clear so here is the updated example: http://jsfiddle.net/jalxob/hEusd/5/

$('input[type="checkbox"]').click(function(){
if($(this).val()=="checking"){
    $(".open-box").toggle();
    if("none" == $(".open-box").css("display")) { 
        $(".box1").css("display", "none");
    }
}
});

$('.open-box').click(function() {
$(".box1").slideToggle();
});
user2989327
  • 75
  • 1
  • 1
  • 8
  • Alright, I solved it again after you clarified. Welcome to StackOverflow by the way. – digitalextremist Dec 06 '13 at 19:52
  • This isn't getting any clearer, could you draw an image (Paint will do) that describes the problem? – Popnoodles Dec 06 '13 at 19:52
  • Ok on the new example: http://jsfiddle.net/jalxob/hEusd/5/ When you check the box it shows a link when you click on it, a new div appears. But when you uncheck the box the new div stills there. Two guys already solved it but they skip the input's value: http://jsfiddle.net/digitalextremist/Jm4L6/ – user2989327 Dec 06 '13 at 20:02
  • There's the 3rd one: http://jsfiddle.net/digitalextremist/Jm4L6/2/ – digitalextremist Dec 06 '13 at 20:15

4 Answers4

0

In your jsfiddle, box2 isn't inside box1. If you put it inside box1, then it will get hidden. If you need to leave it outside, add something like this:

$('input[type="checkbox"]').click(function(){
    if($(this).val()=="checking"){
        $(".box1").toggle();
        if("none" == $(".box1").css("display")) { // ADD THIS IF BLOCK
            $(".box2").css("display", "none");
        }
    }
});

$('.open-box').click(function() {
    $(".box2").slideToggle();
});
elixenide
  • 44,308
  • 16
  • 74
  • 100
0

You need to put both the boxes inside a container, and change it so that clicking the checkbox toggles the container div.

see example here: JSFiddle

I changed the HTML to put both boxes in a div like this:

<input type="checkbox" value="checking">
<div class="box-container">
    <div class="box1">
        <a href="#" class="open-box">Show next box</a>
    </div>
    <div class="box2"></div>
</div>

and changed the checkbox click event to toggle the container:

$(".box-container").toggle();
soupy1976
  • 2,787
  • 2
  • 26
  • 34
0

This solves it, and it's an easy fix: http://jsfiddle.net/digitalextremist/2PcWV/2

<input type="checkbox" value="checking">

<div class="box1">
    <a href="#" class="open-box">Show next box</a>
    <div class="box2"></div>
</div>

UPDATE:

After you clarified, here is a new snippet: http://jsfiddle.net/digitalextremist/Jm4L6/1

That is this:

$('input[type="checkbox"]').click(function () {
    if (this.checked) {
        $(".open-box").show();
    } else {
        $(".open-box").hide();
        $(".box1").hide();
    }
});

$('.open-box').click(function () {
    $(".box1").slideToggle();
});

With this HTML:

<input type="checkbox"> <a href="#" class="open-box">Show next box</a>

<div class="box1"></div>

It involves ideas talked about here: How to check whether a checkbox is checked in jQuery?

This third update ought to cover everything you asked:

http://jsfiddle.net/digitalextremist/Jm4L6/2/

Community
  • 1
  • 1
digitalextremist
  • 5,952
  • 3
  • 43
  • 62
0

You shouldnt use $(this).val()=="checking" as it will always return true whether the checkbox is checked or not.

I would go with something like this :

http://jsfiddle.net/hEusd/12/

JCHebert
  • 83
  • 5