I’m using PHP / MySQL / javascript (very new to javascript)
I’m looking to set the value of a textfield based on the name (NOT value) of a drop-down. The drop-down is created dynamically from a recordset (see code below):
Actually, you don’t even need to loop through them, because that’s only if you want to set a drop-down based on some text.
Setting a field field based on a drop-down is relatively simple by comparison.
Here is a simple form with a drop down and a text field:
The script gets a reference to the form, and from there to the drop down.
It then attaches an onclick event to the drop-down. That attached function sets the text field value to the text of the currently-selected option.
var form = document.getElementById('choosePet'),
pets = form.elements.pets;
pets.onchange = function () {
var form = this.form,
pets = this,
pet = form.elements.pet;
pet.value = pets.options[pets.selectedIndex].text;
};