How to use multiple conditions within an if

3

I have tried in many ways, grouping with () of all the ways that I imagined, creating a variable and calling it in the condition etc, but I can find where I am wrong.

$(function() {
    $('#id1').change(function(){
       if (($(this).val() == '3' || '4') && ('id2' == '5')) {
       $('#div1').fadeIn('slow');
       $('#div2').hide();
        }
       else if ($(this).val() == '6' || '7') {
           $('#div1').hide()
       }
        else {
           $('#div2').hide();
           $('#div1').hide();
        }
    });
});

I have tried this too:

var tip = document.getElementById("#id2").val;
       if (($(this).val() == '3' || '4') && (tip == '5'))

And grouping different:

if ($(this).val() == '3' || '4' && 'id2' == '5')

Or

if (($(this).val() == '3' || '4') && 'id2' == '5')

Follow the link with the example.

    
asked by anonymous 19.07.2015 / 07:46

1 answer

7

Logical operators evaluate the booleans returned in the predicates, they should be used between two expressions (which result in a boolean), or boolean itself (true or false)

You're wrong in this condition:

($(this).val() == '3' || '4')

it should look like this:

($(this).val() == '3' || $(this).val() == '4')

Here's the fix for your fiddle: link

Notes on your fiddle:

1st error

The var tip = document.getElementById("#id2").val; line is wrong, because in order to retrieve the value of a select list using javascript one must return the value of the selected option, so in this case it would be:

var tip = document.getElementById("id2").options[e.selectedIndex].value

or using jquery

var tip = $("#id2").val();

2nd error

The conditions as already mentioned in this answer

3rd error

The event should be captured when any of the two dropdowns is modified, then one more selector must be added to the event

$('#id1, #id2').change(function(){}

Since the event can now be called in any of the dropdowns we must change in the condition where we use $(this).val() , for $('id1').val()

    
19.07.2015 / 07:50