Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

simplifying javascript

Posted on 2015-02-05
2
Medium Priority
?
69 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 44

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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Suggested Courses

618 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