Richlewis
Richlewis

Reputation: 15374

Using || in a Jquery comparison

Seems as if I cant use || in my Jquery script for conducting some comparisons. What I have so far is

var originaltext = [];
var novice = 'Novice'
var intermediate = 'Intermediate'
var expert = 'Expert'

// Changes text on hover
$(function() {
    $('.skillsDouble li').hover(function(){
      originaltext[$(this).index('.skillsDouble li')] = $(this).text();

      if(originaltext[$(this).index('.skillsDouble li')] == 'Selenium') {
        $(this).text(expert);
      }

      else if(originaltext[$(this).index('.skillsDouble li')] == 'CSS') {
        $(this).text(intermediate);
      }

      else {
        $(this).text(novice);
      }

    },
    function(){
       $(this).text( originaltext[$(this).index('.skillsDouble li')]);
    });
});

So a simple function that will take the text of what I am hovering over and compare it to a string I give it, depending on the string given it will output a different string on hover. Where I am falling down (and you may see more, maybe some re factoring to be more efficient) is comapring multiple stings

so when i try this

 if(originaltext[$(this).index('.skillsDouble li')] == 'Selenium' || 'Jmeter')

the function will fail and all hover statuses have the text Novice show.

How can i compare against multiple strings in this statement and if anyone has any tips on re factoring then please share.

Any help appreciated

Thanks

Upvotes: 1

Views: 73

Answers (3)

AllTooSir
AllTooSir

Reputation: 49372

You can correct your statement as (also use === instead of == to avoid implicit casting):

if(originaltext[$(this).index('.skillsDouble li')] === 'Selenium' || 
   originaltext[$(this).index('.skillsDouble li')] === 'Jmeter')

Or this ,

var skills = originaltext[$(this).index('.skillsDouble li')];
if (skills === 'Selenium' || skills === 'Jmeter') 
{
}

Or you can use an array and check for the string in the array :

var strings = ["Selenium", "Jmeter"];
if(strings.indexOf(originaltext[$(this).index('.skillsDouble li')]) > -1)

Upvotes: 1

Sushanth --
Sushanth --

Reputation: 55750

if(originaltext[$(this).index('.skillsDouble li')] == 'Selenium' || 'Jmeter')

has to be written like this

if(originaltext[$(this).index('.skillsDouble li')] == 'Selenium' 
   || originaltext[$(this).index('.skillsDouble li')] == 'Jmeter')

Or better

var original = originaltext[$(this).index('.skillsDouble li')];
if(original  === 'Selenium' || original  === 'Jmeter')

Always a better idea to Not to repeat yourself. And use === instead of == where ever possible as the former performs better than the latter.

Upvotes: 1

Darren
Darren

Reputation: 70728

You need:

 if(originaltext[$(this).index('.skillsDouble li')] == 'Selenium' || originaltext[$(this).index('.skillsDouble li')] == 'Jmeter') 

I would recommend storing originaltext[$(this).index('.skillsDouble li')] into a variable and then comparing it, it makes it more readable.

For instance:

var skillsDouble = originaltext[$(this).index('.skillsDouble li')];

if (skillsDouble == 'Selenium' || skillsDouble == 'Jmeter') 
{

}

Upvotes: 1

Related Questions