Solved

Checking a Drop Down Value against a Javascript Numeric Array List

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
date format angularjs2 16 43
Wordpress Query 1 35
HTML page and JavaScript 2 54
Angular - (very close) following an example 20 36
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, …
This article discusses how to create an extensible mechanism for linked drop downs.
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 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)

759 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now