Steve Kim
Steve Kim

Reputation: 5591

jQuery combine var into a string

So, I have the following js:

var first = jQuery(this).data("first");     
var second = jQuery(this).data("second");  
var third = jQuery(this).data("third "); 

if(typeof first !== 'undefined' && first != ''){
   alert ("Done");
};
if(typeof second !== 'undefined' && second != ''){
   alert ("Done");
};
if(typeof third !== 'undefined' && third != ''){
   alert ("Done");
};

Here, regardless which variable is available, it will show the alert as all of them satisfy the condition.

To reduce the redundancy, I was thinking of making a var that contains all three variables then use this total variable to meet the condition:

var first = jQuery(this).data("first"); // has character 1      
var second = jQuery(this).data("second");  // has character 2
var third = jQuery(this).data("third "); // has character 3

var total = first, second, third; ?????

if(typeof total !== 'undefined' && total != ''){
   alert ("Done");
};

So, if one of the variables is available then, it meets the condition.

Of course the above code doesn't work. Could someone give me a hand? Thanks bunch!

Steve

Upvotes: 0

Views: 81

Answers (5)

Arman Ozak
Arman Ozak

Reputation: 2344

If you have too many variables to do an "OR" check you can do this:

var arr = [];
arr.push(jQuery(this).data("first"));    
arr.push(jQuery(this).data("second"));
arr.push(jQuery(this).data("third"));
arr.push(jQuery(this).data("fourth"));
arr.push(jQuery(this).data("fifth"));
arr.push(jQuery(this).data("sixth"));
arr.push(jQuery(this).data("seventh"));
arr.push(jQuery(this).data("eighth"));
// add any other here

if(arr.join('').length){
    console.log('Done');
}

Upvotes: 1

Arun P Johny
Arun P Johny

Reputation: 388316

Another way to do it will be to use an array and Array.some() to check if any one property matches the condition like

var self = this,
  flag = ['first', 'second', 'third'].some(function(item) {
    var val = jQuery(this).data(item);
    return val !== undefined && val !== '';
  });

if (flag) {
  alert("Done");
};

Upvotes: 1

Mi-Creativity
Mi-Creativity

Reputation: 9654

JS Fiddle

var first = jQuery(this).data("first"); // has character 1      
var second = jQuery(this).data("second");  // has character 2
var third = jQuery(this).data("third "); // has character 3

var checkArray = [first, second, third];

if(typeof total !== 'undefined' && $.inArray(total, checkArray) > -1){
    console.log("Done");
};

Upvotes: 1

StudioTime
StudioTime

Reputation: 23959

Like this?

switch(true){
 case (typeof first !== 'undefined' && first != ''):
   alert('done first');
   break;
 case (typeof second!== 'undefined' && second != ''):
   alert('done second');
   break;
 case (typeof third!== 'undefined' && third != ''):
   alert('done third');
   break;
 default:
   alert('not done');
}

Upvotes: 1

user2314737
user2314737

Reputation: 29307

Use this to check if at least one of the three variables is not null/not empty

--or if (first && second && third) if you want all three to be not null.

var first = jQuery(this).data("first"); // has character 1      
var second = jQuery(this).data("second"); // has character 2
var third = jQuery(this).data("third "); // has character 3


if (first || second || third) {
  console.log("Done");
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 1

Related Questions