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.
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:
Each of the pair has class “flap” on it. Then I can write a function:
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:
Much more concise! Thanks.