Alex
Alex

Reputation: 1152

Multiple else if Statements in jQuery

        $('#bar3').live('click', function() {
            if($('#bar3').attr('class') == '0') {
                console.log("closed")
            } else if($('#bar1' && '#bar2').attr('class') == '0') {
                console.log("Both")
            } else if($('#bar1').attr('class') == '0') {
                console.log("Summary Open")
            } else if($('#bar2').attr('class') == '0') {
                console.log("HIP Open")
            } else {
                console.log("open") 
            }
         });

Why does this not work?

I know what it's doing, I just don't know what to change to fix it.

This part is wrong:

else if($('#bar1' && '#bar2').attr('class') == '0')

Upvotes: 2

Views: 31259

Answers (5)

勿绮语
勿绮语

Reputation: 9320

$('#bar1' && '#bar2').attr('class') == '0'

should be

$('#bar1').attr('class') == "0" &&  $('#bar2').attr('class') == "0"

&& is a valid java script operator, but not valid as part of a jquery selector.

Upvotes: 5

Dmitriy
Dmitriy

Reputation: 11

They aren't actually hardcoded. I grabbed that code from the the property inspector and not my HTML file, sorry bout that! The class is dynamically added by TinyAccordion script – Alex Dec 15 '11 at 2:19

You only need to change in TinyAccordion this code:

var parentAccordion=new TINY.accordion.slider("parentAccordion");
parentAccordion.init("acc","h3",0,0);

to this:

var parentAccordion=new TINY.accordion.slider("parentAccordion");
parentAccordion.init("acc","h3",0,0,"your_class_name");

Upvotes: 1

redShadow
redShadow

Reputation: 6777

Try replacing

} else if($('#bar1' && '#bar2').attr('class') == '0') {

with:

} else if ( ($('#bar1').attr('class') == '0') && ($('#bar2').attr('class') == '0')) {

UPDATE:

I just tried this code, that is working on jsfiddle:

$('#bar3').live('click', function() {
    if ($('#bar1').hasClass("0") && $('#bar2').hasClass("0")) {
        alert("Both bars are Open");
    } else if ($('#bar1').hasClass("0")) {
        alert("Bar 1 is Open");
    } else if ($('#bar2').hasClass("0")) {
        alert("Bar 2 is Open");
    } else {
        alert("No bars are open");
    }
});

Upvotes: 0

Jon
Jon

Reputation: 437376

Because you are simply using the wrong syntax. This:

$('#bar1' && '#bar2').attr('class') == '0'

would need to be written more like

$('#bar1').attr('class') == '0' && $('#bar2').attr('class') == '0'

But that still leaves major issues:

  • What is .attr('class') == '0' supposed to do? Do you have a class named 0? If so, the correct way would be .hasClass('0').
  • You keep re-wrapping DOM elements in jQuery objects. Do it only once and store the results, e.g. var $bar1 = $('#bar1') and then use $bar1 again and again.

Upvotes: 3

Matt Lacey
Matt Lacey

Reputation: 8255

You're not checking two conditions, you're using && with two strings, and passing the result of that to jQuery. You want to do this:

else if($('#bar1').attr('class') == '0' && $('#bar2').attr('class') == '0')

Upvotes: 0

Related Questions