?
Solved

simplifying javascript

Posted on 2015-02-05
2
Medium Priority
?
63 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 22

Accepted Solution

by:
plusone3055 earned 2000 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 43

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

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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)
Suggested Courses

764 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