Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
109
rated 0 times [  110] [ 1]  / answers: 1 / hits: 33614  / 11 Years ago, thu, august 1, 2013, 12:00:00

Is there a way to make the following JavaScript if..else condition short, instead of writing if() else() function two times is there any way to write if(x,y = ){} else{} or something similar to make it shorter ?



$(document).ready(function(){
$(#third).click(function(){
var xxx = $(#first).val();
var xxy = $(#second).val();

if (xxx == ) {
alert(good);
}
else {
alert(bad);
}

if (xxy == ) {
alert(good);
}
else {
alert(bad);
}
});
});

More From » javascript

 Answers
10

Use the or syntax (||)



if(xxx == '' || yyy == '')
{
alert(good);
}
else
{
alert(bad);
}


This way, if xxx is an empty string OR yyy is an empty string, it will alert good. If neither of them are empty, it will alert bad


[#76597] Wednesday, July 31, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
darrylm

Total Points: 499
Total Questions: 131
Total Answers: 108

Location: Saudi Arabia
Member since Mon, Sep 5, 2022
2 Years ago
;