tags:

views:

145

answers:

3

I have a number of message elements that come in pairs: If element A1 is shown, then A2 should be hidden, same for B1/B2, C1/C2, and so on. I have the jQuery code working, but it gets verbose:

if (conditionA) {
    $("#a1").show();
    $("#a2").hide();
else {
    $("#a1").hide();
    $("#a2").show();
}

if (conditionB) {
    $("#b1").show();
    $("#b2").hide();
else {
    $("#b1").hide();
    $("#b2").show();
}

//etc...

This seems cumbersome and mind-numbing. Is there a better way to encapsulate the notion that these elements are paired and should show/hide opposite each other? I've looked at toggle, but that isn't right.

A: 

I would organize your html so that you could use siblings() and toggle().

contagious
Can you show an example? Keep in mind, I don't want to necessarily change the state of the divs at all: my understanding of toggle was that it examined the current state, and flipped between hide and show. I might run through my code and the conditions are all the same as last time, and nothing is changed.
Ned Batchelder
+3  A: 

Actually toggle can help you here, if you make use of the optional switch parameter.

$("#a1").toggle(conditionA);
$("#a2").toggle(!conditionA);

$("#b1").toggle(conditionB);
$("#b2").toggle(!conditionB);
Alex Barrett
Ah! I had misunderstood what the parameter to toggle meant.
Ned Batchelder
The `!`'s should be reversed there. Toggle *hides* the element if the switch is `true`, not the other way around.
Crescent Fresh
My apologies, perhaps I should have checked the documentation myself! Fixed.
Alex Barrett
+1. Didn't know about this feature. Cool.
bigmattyh
After using this functionality myself in one of my own projects, it seems the jQuery documentation is incorrect on this matter and toggle *shows* the element if switch is `true`.I have reverted my changes to the comment and filed a bug report: http://dev.jquery.com/ticket/5153
Alex Barrett
A: 

Here's what I ended up doing:

I organized the HTML to have the pair of elements paired under a new parent for naming purposes:

<p id="first_flap"><span class="flap">MsgA1</span><span class="flap">MsgA2</span></p>
<p id="second_flap"><span class="flap">MsgB1</span><span class="flap">MsgB2</span></p>

Each of the pair has class "flap" on it. Then I can write a function:

function flip_flap(sel, cond) {
    /* Find sel, then show flaps within it according to cond. */
    var flaps = jQuery(sel + ">.flap");
    var f0 = jQuery(flaps[0]);
    (cond ? f0.show() : f0.hide());
    var f1 = jQuery(flaps[1]);
    (cond ? f1.hide() : f1.show());
}

I liked the idea of using toggle(), but unfortunately, it doesn't work for inline elements, only block level, and I needed to use spans.

Then I can replace my original Javascript with:

flip_flap("#first_flap", conditionA);
flip_flap("#second_flap", conditionB);

Much more concise! Thanks.

Ned Batchelder
`toggle` routes to `hide` and `show`, which handle both block and inline elements (at least in 1.3.2). What version are you using where you cannot toggle inline elements?
Crescent Fresh
Hmm, looks like I'm using 1.2.6, maybe I should upgrade...
Ned Batchelder