Solved

Checking a Drop Down Value against a Javascript Numeric Array List

Posted on 2010-09-16
3
406 Views
Last Modified: 2013-11-18
I would like to check an drop down list value on a form page with a numeric list to see if the value is in the list.
How can this be done via Javascript?

For example:
 
var myList = new Array(2,3,14,25); // the id list to check against

var fieldVal = thing[thing.selectedIndex].value; // the numeric selected drop down field value


if(fieldVal == myList[idx])
    alert("Yes the drop down value is in my array list");
else
    alert("No. the drop down value is not in the array list");

0
Comment
Question by:FastEddie___
[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
3 Comments
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
ID: 33695544
Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--) {
        if (this[i] == obj) {
            return true;
        }
    }
    return false;
}

var myList = new Array(2,3,14,25); // the id list to check against
var fieldVal = parseInt( thing[thing.selectedIndex].value );

if( myList.contains(fieldVal) )
    alert("Yes the drop down value is in my array list");
else
    alert("No. the drop down value is not in the array list");
0
 
LVL 2

Expert Comment

by:agileblowfish
ID: 33695557

for (var i=0; i < myList.length; i++) {
	if (fieldVal == myList[i]) {
		alert("Yes the drop down value is in my array list");
	}else{
		alert("No. the drop down value is not in the array list");
	}
}

Open in new window

0
 

Author Closing Comment

by:FastEddie___
ID: 33697130
Thanks
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Anyone familiar with PhotoSwipe? 3 42
Why am I getting ReferenceError:  boxes is not defined error 5 33
javascript delete path 7 44
allow link tag to be submit like form 15 60
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

734 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