How to use Switch Case in jQuery

A switch works by comparing what is in switch() to every case.
[javascript]
switch (cnt) {
    case 1: ….
    case 2: ….
    case 3: ….
}
[/javascript]

works like:
[javascript]
if (cnt == 1) …
if (cnt == 2) …
if (cnt == 3) …
[/javascript]

Therefore, you can’t have any logic in the case statements.
[javascript]
switch (cnt) {
    case (cnt >= 10 && cnt <= 20): … } [/javascript]

works like [javascript] if (cnt == (cnt >= 10 && cnt <= 20)) …
[/javascript]

and that’s just nonsense. :)

Use if () { } else if () { } else { } instead.



  • My Experience

    My expertise are primarily in C#, ASP.NET, HTML, JavaScript, SQL, SEO, Windows Services, XML and Web services. Areas of technical expertise are in Web application design and architecture and security.
  • Quote of the Day !!

    "Be nice to nerds. Chances are you'll end up working for one." – Bill Gates