0

Consider the following code, which toggles visibility of two classes with separate click() functions:

<!-- Toggles -->
<div class="a"></div>
<div class="b"></div>

<!-- Result -->
<div class="x" style="display:none"></div>
<div class="y" style="display:none"></div>
<div class="z" style="display:none"></div>

<!-- Script -->
$( ".a" ).click(function() {
var $this = $(this);
  $this.siblings(".x").toggle();
});


$( ".b" ).click(function() {
var $this = $(this);
  $this.siblings(".y").toggle();
});

How would I update this so that any time both x and y are visible, a third class, "z" is shown instead of both x and y?

0

3 Answers 3

1

Demo http://jsfiddle.net/Yn3L2/

Rest should fit your needs :)

Code

$(".a").click(function () {
    var $this = $(this);
    $this.siblings(".x").toggle();
    checkZ();
});

$(".b").click(function () {
    var $this = $(this);
    $this.siblings(".y").toggle();
    checkZ();
});

function checkZ() {
    $('.z').hide();
    if ($('.x').is(':visible') && $('.y').is(':visible')) {

        $('.z').show();
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

@Tats_innit thanks, if I want to keep this relational to the $this var (i.e. be able to use this multiple times on the same page, but only trigger the function on sibling classes, would if ($this.siblings(".a").is(':visible') && () $this.siblings(".b").is(':visible')) work?
@alias51 yeah sure you can :) pass this as a function parameter, stackoverflow.com/questions/10198768/… please note you need to know the visibility status of both x and y at all time though! please see this demo jsfiddle.net/VxwUR passing value as parameter
@Tats_innit, thanks, I'm having difficulty following how to pass this as a function parameter; I am using checkZ($this) but still get an uncaught reference error?
0

This works as show here: http://jsfiddle.net/DKRe2/1/

HTML:

<!-- Toggles -->
<div class="a">a</div>
<div class="b">b</div>
<!-- Result -->
<div class="x" style="display:none">class x</div>
<div class="y" style="display:none">class y</div>
<div class="z" style="display:none">class z</div>

JS:

<!-- Script -->
$(".a").click(function () {

    var $this = $(this);
    if ($this.siblings(".y").css('display') != 'none' && $this.siblings(".x").css('display') == 'none') {
        //now Hide y and show Z  
        $this.siblings(".y").toggle();
        $this.siblings(".z").toggle();
    } else {
        $this.siblings(".z").css('display', 'none');
        $this.siblings(".x").toggle();
    }
});


$(".b").click(function () {
    var $this = $(this);
    if ($this.siblings(".x").css('display') != 'none' && $this.siblings(".y").css('display') == 'none') {
        //now Hide y and show Z   
        $this.siblings(".x").toggle();
        $this.siblings(".z").toggle();
    } else {
        $this.siblings(".z").css('display', 'none')
        $this.siblings(".y").toggle();
    }
});

2 Comments

Thanks, how would this work if I wanted to keep the code relational (i.e. by reference to sibilings), so only siblings were affected (classes are repeated elsewhere on the page)?
Just add back the siblings selector from before. I removed it as it wasn't strictly necessary in the code example you gave. I will amend answer.
0

I think this is what you really want.

Working DEMO

Added jQuery code

function checkZ() {
    $('.z').hide();
    if ($('.x').is(':visible') && $('.y').is(':visible')) {
        $('.x').hide(500);
        $('.y').hide(500)
        $('.z').show();
    }
}

2 Comments

Thanks, how would this work if I wanted to keep the code relational (i.e. by reference to sibilings), so only siblings were affected (classes are repeated elsewhere on the page)?
BY probably providing a unique Id to the child div and referecing the ID in the jQuery

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.