Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Create a switch statement from an if (jQuery) Re: Create a switch statement from an if (jQuery)

#137359
BaliBalo
Participant

Hi.
You can do it this way:

switch(response)
{
case ‘standard’:
window.location.href = “/client-area/”;
break;
case ‘worldstores’:
window.location.href = “/client-area/worldstores/”;
break;
case ‘wilko’:
window.location.href = “/client-area/wilko/”;
break;
default:
$(“#message”).html(“Sorry your username or password was entered incorrectly.”);
}

(I removed the p tag in the error message because the formatting was messed up)
But there are other ways to do it:

var urls = {
standard: ‘/client-area/’,
worldstores: ‘/client-area/worldstores/’,
wilko: ‘/client-area/wilko/’
};
if(response in urls)
window.location.href = urls[response];
else
$(‘#message’).html(‘Sorry your username or password was entered incorrectly.’);

Do what you think is the more understandable.