Solved

simplifying javascript

Posted on 2015-02-05
2
62 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 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 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

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…
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.
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)
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…

691 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