Mary
Mary

Reputation: 365

Logical operator OR problem: jQuery

Or operator does not work. could you help how to get it right.

$('.iconWrapper span').click(function(e) {
   $('#div1').find('img').attr('src', function(index, src) {
 if( src =='../../photo/roz1.jpg' || '../../photo/roz2.jpg'){
        alert ('ohra');
        }else{
        alert ('lil');
 }

});

Upvotes: 1

Views: 3975

Answers (4)

Tim Büthe
Tim Büthe

Reputation: 63814

In JavaScript || is used for or. Your if should read something like this:

if( src == '../../photo/roz1.jpg' || src == '../../photo/roz2.jpg'){

You really need to leran the basiscs, checkout these as a starting point:

Upvotes: 2

El Ronnoco
El Ronnoco

Reputation: 11912

if( src =='../../photo/roz1.jpg' || src == '../../photo/roz2.jpg'){...

Upvotes: 3

Bing
Bing

Reputation: 746

You'll need to add "src==" after or as well, and use || for or.

Upvotes: 1

Felix Kling
Felix Kling

Reputation: 817148

It "does not work" because this operator does not exist in JavaScript (why do you think it does?)

It is ||, and you have to test the condition explicitly: var == val1 || var == val2.

var == (val1 || val2) is doing something different.

if(src =='../../photo/roz1.jpg' || src == '../../photo/roz2.jpg')

See MDC - Logial Operators.

Upvotes: 5

Related Questions