Solved

Checking a Drop Down Value against a Javascript Numeric Array List

Posted on 2010-09-16
3
404 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___
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
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…

803 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