Is there an 'else if' in Javascript/jQuery?


#1

I’m trying to target only one if statement (either this or that), and else if would work in this case but I’m sure if Javascript has an ‘else if’?

Kinda confusing but I’m “in the learning process”… :stuck_out_tongue:


#2
if () {

}
else if (){

}else

{

#3

Hey,
There is “else if” function in javascript example of it’s use “here”:http://www.learningjquery.com/2007/08/clearing-form-data

If you want an alternative there is a “plugin for jQuery”:http://webreflection.blogspot.com/2008/10/jquery-if-elseif-and-else-plugin.html


#4

You can use basic javascript if else :wink:


#5

The “else if” statement in JS is like writing nested if/else statements but the else statements don’t have brackets making it on one line. :slight_smile:

if (...) {
     
} else {
     if (...) {

     } else {
          if (...) {
          
          }
     }
}

#6
Hey, There is "else if" function in javascript example of it's use "here":http://www.learningjquery.com/2007/08/clearing-form-data

If you want an alternative there is a “plugin for jQuery”:http://webreflection.blogspot.com/2008/10/jquery-if-elseif-and-else-plugin.html

Wow, I can't believe someone made a plugin for basic conditional statements, that's crazy.

@Vasilios - I think your example is more complicated than it needs to be :slight_smile:

basic else if:


var myNum = 10;
if (myNum > 10) {
   alert('Greater than 10.');
} 
else if (myNum < 2) {
   alert('Less than 2.');
}
else {
    alert(myNum);
}

switch statement

Or use a switch statement (as long as you understand how they work, certain cases can fall through to another if you aren’t careful):


switch(someVariable) {
   case 1:
      block of code;
      break;
   case 2:
      block of code;
      break;
   default:
      block of code;
}


#7

^ Yeah. XD My book examples it better than I do.


#8
The "else if" statement in JS is like writing nested if/else statements but the else statements don't have brackets making it on one line. :)
if (...) {
     
} else {
     if (...) {

     } else {
          if (...) {
          
          }
     }
}
GET ON AIM, I need your help!!! :)

…puh-lease :slight_smile:


#9
The "else if" statement in JS is like writing nested if/else statements but the else statements don't have brackets making it on one line. :)
if (...) {
     
} else {
     if (...) {

     } else {
          if (...) {
          
          }
     }
}
GET ON AIM, I need your help!!! :)

…puh-lease :slight_smile:

Like I said, you really don't need to write it that way.

#10
The "else if" statement in JS is like writing nested if/else statements but the else statements don't have brackets making it on one line. :)
if (...) {
     
} else {
     if (...) {

     } else {
          if (...) {
          
          }
     }
}
GET ON AIM, I need your help!!! :)

…puh-lease :slight_smile:

Like I said, you really don't need to write it that way.

I was just writing that showing how it would normally be written without else if. :slight_smile:


#11

Drew, I tried else if’s, but I guess they didn’t work as I hoped they would. In my case, they work the same as just having a bunch of if statements in a row.

It doesn’t filter out just one statement, it still goes through all of them, even with else, and else if.


#12
Drew, I tried else if's, but I guess they didn't work as I hoped they would. In my case, they work the same as just having a bunch of if statements in a row.

It doesn’t filter out just one statement, it still goes through all of them, even with else, and else if.

Then I would assume their is a problem with your logic or code somewhere. A link would probably help us help you :)

#13

*Edit: http://www.pastie.org/556812

I’m trying to write a resizing script. I actually wrote one that worked fine in Safari and Firefox, but IE8 failed to make it work so I had to rewrite it.

The goal of this script is to be able to resize any image ratio (no matter how crazy) to fit into a 720 by 480 container. The whole image has to show aswell.

Right now, the code works for everything except images that are over 480px tall and ridiculously wide.

Help would be pretty freakin’ sweet. :slight_smile:


#14

Just so you know, a comma is not the logical AND. AND is &&.


#15
Just so you know, a comma is not the logical AND. AND is &&.
I'm a beginner. :)

#16

Just wanted to let you know because that might have been the problem. :slight_smile:


#17
Just wanted to let you know because that might have been the problem. :)
Nah, the comma worked just fine actually. :)

#18
*Edit: http://www.pastie.org/556812

I’m trying to write a resizing script. I actually wrote one that worked fine in Safari and Firefox, but IE8 failed to make it work so I had to rewrite it.

The goal of this script is to be able to resize any image ratio (no matter how crazy) to fit into a 720 by 480 container. The whole image has to show aswell.

Right now, the code works for everything except images that are over 480px tall and ridiculously wide.

Help would be pretty freakin’ sweet. :slight_smile:

A few things to note:
  1. Try debugging your JS using firebug. You will find that on line 11 you have an error with image, which is undefined. The alert command is never run at this point.

  2. You really should be using ‘;’ at the end of your line executions. Even though they aren’t required in JS, it is bad practice and a bad idea. Javascript does some wacky things when you leave it up to itself to insert semicolons.

Like I said, try debugging your JS with firebug, I think it will help you out here :slight_smile:


#19
*Edit: http://www.pastie.org/556812

I’m trying to write a resizing script. I actually wrote one that worked fine in Safari and Firefox, but IE8 failed to make it work so I had to rewrite it.

The goal of this script is to be able to resize any image ratio (no matter how crazy) to fit into a 720 by 480 container. The whole image has to show aswell.

Right now, the code works for everything except images that are over 480px tall and ridiculously wide.

Help would be pretty freakin’ sweet. :slight_smile:

A few things to note:
  1. Try debugging your JS using firebug. You will find that on line 11 you have an error with image, which is undefined. The alert command is never run at this point.

  2. You really should be using ‘;’ at the end of your line executions. Even though they aren’t required in JS, it is bad practice and a bad idea. Javascript does some wacky things when you leave it up to itself to insert semicolons.

Like I said, try debugging your JS with firebug, I think it will help you out here :slight_smile:

...Thanks. I guess I didn't myself clear -- this isn't all of the code. This script is part of another huge script that works fine, that's why image isn't defined. It's defined earlier, which is irrelevant to _this_ script.

Anyway, I’ll just figure it out myself. Doesn’t seem like anyone here has seen an image resizing script in their lives…

Thanks for everyone’s help, I appreciate it!


#20
*Edit: http://www.pastie.org/556812

I’m trying to write a resizing script. I actually wrote one that worked fine in Safari and Firefox, but IE8 failed to make it work so I had to rewrite it.

The goal of this script is to be able to resize any image ratio (no matter how crazy) to fit into a 720 by 480 container. The whole image has to show aswell.

Right now, the code works for everything except images that are over 480px tall and ridiculously wide.

Help would be pretty freakin’ sweet. :slight_smile:

A few things to note:
  1. Try debugging your JS using firebug. You will find that on line 11 you have an error with image, which is undefined. The alert command is never run at this point.

  2. You really should be using ‘;’ at the end of your line executions. Even though they aren’t required in JS, it is bad practice and a bad idea. Javascript does some wacky things when you leave it up to itself to insert semicolons.

Like I said, try debugging your JS with firebug, I think it will help you out here :slight_smile:

...Thanks. I guess I didn't myself clear -- this isn't all of the code. This script is part of another huge script that works fine, that's why image isn't defined. It's defined earlier, which is irrelevant to _this_ script.

Anyway, I’ll just figure it out myself. Doesn’t seem like anyone here has seen an image resizing script in their lives…

Thanks for everyone’s help, I appreciate it!

That last line is a little insulting, I have built an entire image gallery manager and resizer, so I do know all about image resizing, and it really should be done on the server side anyway if you are going to be scaling a bunch of images.

Also, how are we supposed to know that is not all the code if you didn’t give it?

No matter what I still suggest you start following best coding practices and ending your line statements with ‘;’

Best of luck with your script :slight_smile: