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

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:

if () {

}
else if (){

}else

{

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

You can use basic javascript if else :wink:

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 (...) {
          
          }
     }
}
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;
}

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

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:

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.
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:

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.

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 :)

*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:

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

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

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

Just wanted to let you know because that might have been the problem. :)
Nah, the comma worked just fine actually. :)
*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:

*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!

*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: