Solved

simplifying javascript

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Accordion won't close correctly 5 14
How to add custom html-5 element to a dynamically generated link? 4 24
Calculating percentage 2 30
Problem in javascript 17 26
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
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 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…

733 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