Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 19th, 2005, 01:09 AM
SoC SoC is offline
Authorized User
 
Join Date: Jul 2004
Posts: 65
Thanks: 0
Thanked 0 Times in 0 Posts
Default Compare values of several drop boxes, notify user

Hi,

I need to create a web page that shows 5 or more drop boxes. Their values will be populated from the same database.

I would like to alert the user when they select the same option in 2 or more drop boxes.

For example, if the user selects 'Pig' from box 1, 'Dog' from box 2, 'Pig' from box 3, the selected option in boxes 1 and 3 will change to red text, and an alert will pop up notifying them of the clash.

I can achieve this functionality across 2 drop boxes simply by comparing their values on the onChange event, but I'm not sure how to do it when their are more than 2 drop boxes.

Do I have to use an array or something? Any help much appreciated.

S

 
Old April 21st, 2005, 10:17 AM
Friend of Wrox
 
Join Date: May 2004
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

This sort of works.


<html>

<head>
<title>Array Experiment</title>
</head>

<body>

<form name="form1">
<select name="sel1" id="1st" onchange="checkforduplicates()">
<option value="pig">Pig
<option value="dog">Dog
<option value="monkey">Monkey</option>
</select> <select name="sel2" id="2nd" onchange="checkforduplicates()">
<option value="dog">Dog
<option value="monkey">Monkey
<option value="pig">Pig</option>
</select> <select name="sel3" id="3rd" onchange="checkforduplicates()">
<option value="giraffe">Giraffe
<option value="Pig">Pig
<option value="zebra">Zebra</option>
</select> <select name="sel4" id="4th" onchange="checkforduplicates()">
<option value="wildebeest">Wildebeest
<option value="pigeon">Pigeon
<option value="Pig">Pig</option>
</select> <select name="sel5" id="5th" onchange="checkforduplicates()">
<option value="blackbird">Blackbird
<option value="pig">Pig
<option value="lemur">Lemur</option>
</select>
</form>

<script language ="javascript">
var timer1 = setTimeout("checkforduplicates()", 20)

function checkforduplicates(){

var checksel = {
checksel1 : document.form1.sel1,
checksel2 : document.form1.sel2,
checksel3 : document.form1.sel3,
checksel4 : document.form1.sel4,
checksel5 : document.form1.sel5
}


var holdingpen = new Array(checksel.checksel1.options(checksel.checksel 1.selectedIndex).value, checksel.checksel2.options(checksel.checksel2.sele ctedIndex).value, checksel.checksel3.options(checksel.checksel3.sele ctedIndex).value, checksel.checksel4.options(checksel.checksel4.sele ctedIndex).value, checksel.checksel5.options(checksel.checksel5.sele ctedIndex).value)



holdingpen.sort()

for (var i=0; i<holdingpen.length - 1; i++)
{
  if (holdingpen[i] == holdingpen[i+1])
    //holdingpen[i+1] = '';
    alert(holdingpen[i+1] = holdingpen[i] + ' was duplicated!');
    document.form1.reset();
}

}
</script>


</body>

</html>


Hope that helps
interrupt






Similar Threads
Thread Thread Starter Forum Replies Last Post
Drop Down boxes in Cells and selecting ranges tbmbob Excel VBA 0 June 1st, 2006 09:12 AM
Forms and drop down boxes Apocolypse2005 Javascript 1 December 6th, 2005 11:26 AM
drop down list values based on another drop down noor ASP.NET 1.0 and 1.1 Basics 3 July 5th, 2005 09:57 AM
Assigning Macros to Drop Down Boxes ashu_gupta75 Excel VBA 1 August 12th, 2004 12:44 AM
Two drop down dependent list boxes and a Button tcasp Classic ASP Basics 0 August 5th, 2003 02:06 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.