Solved

simplifying javascript

Posted on 2015-02-05
2
54 Views
Last Modified: 2015-02-05
Is there a simpler way to check IF this field is the correct state?
instead of repeating the OR statement.

       if (  $("#State").val() == "NY" || $("#State").val() == "NJ" ||$("#State").val() == "CA" || $("#State").val() == "MA" || $("#State").val() ==  "IL" )  {
0
Comment
Question by:rivkamak
2 Comments
 
LVL 22

Accepted Solution

by:
plusone3055 earned 500 total points
ID: 40591836
since your only putting in 5 OR's that's really the fastest way as opposed to making an array and looping through the array to check each value. Programmatically the 5 OR's is faster. Now if you you were checking against 100's of values an array would be better
0
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 40591912
You could potentially improve it by only using the jQuery selector once, assigning the value to a variable and then using that in your OR statements. The jQuery selector is calling a function every time, so it's gonna be quicker just calling it once:

var state = $("#State").val();
if (  state == "NY" || state == "NJ" ||state == "CA" || state == "MA" || state ==  "IL" ) 

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now