Update all select boxes on page using Javascript

S

simon.wilkinson

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
 
L

Lee

(e-mail address removed) said:
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.type.match(/select/i)) {
if (box.checked) control.selectedIndex=1;
control.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>


--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top