Connecting Tech Pros Worldwide Help | Site Map

Update all select boxes on page using Javascript

  #1  
Old January 25th, 2007, 12:45 PM
simon.wilkinson@cohenschemist.co.uk
Guest
 
Posts: n/a
Hi,

I am trying to update all Select boxes on a page dynamically using
javascript, I simple want to change the selected item in each select
box when a tick box is pressed on the page. Each Select box is named
in the same convention ie. ddl_DeliveryStatus_ and then the recordID
and contains the same options in the same order. The number of select
boxes changes every time the page is loaded as this is all built using
ASP linked to a database.

I am hoping there is an array or collection or something similar I can
simply reference to do this, but an struggling to find the answer.

Any help would be appreciated.

Thanks

simon

  #2  
Old January 25th, 2007, 03:35 PM
Lee
Guest
 
Posts: n/a

re: Update all select boxes on page using Javascript


simon.wilkinson@cohenschemist.co.uk said:
Quote:
>
>Hi,
>
>I am trying to update all Select boxes on a page dynamically using
>javascript, I simple want to change the selected item in each select
>box when a tick box is pressed on the page. Each Select box is named
>in the same convention ie. ddl_DeliveryStatus_ and then the recordID
>and contains the same options in the same order. The number of select
>boxes changes every time the page is loaded as this is all built using
>ASP linked to a database.
>
>I am hoping there is an array or collection or something similar I can
>simply reference to do this, but an struggling to find the answer.
If the select boxes and the checkbox are in the same form,
you can loop through the elements of the form to find all
of the select boxes. I've exceeded your specs by also
preventing the user from changing the select values while
the box is checked:


<html>
<head>
<title>Select Demo</title>
<script type="text/javascript">
function setSelectsToB(box) {
var control=box.form.elements;
for (var i=0;i<control.length;i++) {
if (control[i].type.match(/select/i)) {
if (box.checked) control[i].selectedIndex=1;
control[i].disabled=box.checked;
}
}
}
</script>
<body>
<form>
<select name="alpha"><option>a</option><option>b</option></select><br>
<select name="beta"><option>a</option><option>b</option></select><br>
<select name="gamma"><option>a</option><option>b</option></select><br>
Force to b<input type="checkbox" onclick="setSelectsToB(this)">
</form>
</body>
</html>


--

  #3  
Old January 25th, 2007, 04:05 PM
simon.wilkinson@cohenschemist.co.uk
Guest
 
Posts: n/a

re: Update all select boxes on page using Javascript


Thanks Lee that worked a treat.

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
A selection changes on asp page with 6 dependent list boxes, when back Galina answers 8 November 18th, 2005 04:20 AM
Codebehind unaware of select box members populated via javascript Allan M. answers 5 November 18th, 2005 01:27 AM
FAQ update (roundup of pending requests - for comment) Richard Cornford answers 42 July 20th, 2005 02:38 PM
HACKING WITH JAVASCRIPT Gowhera Hussain answers 0 July 18th, 2005 12:26 AM