Forum Moderators: open

Message Too Old, No Replies

Code works; but not with an array.

Stumped!

         

chutes

3:22 am on Jul 8, 2010 (gmt 0)

10+ Year Member



<script type="text/javascript">
<!--
function getValue(obj) {
document.forms[0].field1.value = obj.options[obj.selectedIndex].value;

if (obj.selectedIndex==3 || obj.selectedIndex==1){
document.forms[0].field1.value = 999;
}
}
// -->
</script>

<div style="text-align: center;">
<strong>Sample</strong>
</div>

<form action="">
<p style="text-align: center;">

<select name="list1" onChange="getValue(this)">
<option value=""></option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>
</select><br>

<input type="text" name="field1" size="15">
</p>
</form>


In testing environment, it does EXACTLY what I want it to do.

Problem: My site is driven by MySql & PHP. I'm not sure how to tell javascript that "field1" (the input box) is actually "field[73]", "field[21]", "field[56]" etc.

In addition: the select/pull-down is also: "list1[73]", "list[21]", "list[56]" etc.

For example's sake, those are not all of the array #s; they vary from the 20s to 100s based upon the table id in the database. But each list[] matches up with the field[] (same id[] numbers) that I want to populate with 999; once option 1 or option 3 is selected from the pull-down.

daveVk

7:09 am on Jul 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To go to <input type="text" name="field[1]" size="15">

instead of

<input type="text" name="field1" size="15">

function getValue(obj,fldNo) {
var to = document.forms[0].['field['+fldNo+']'];
to.value = obj.options[obj.selectedIndex].value;

if (obj.selectedIndex==3 || obj.selectedIndex==1){
to.value = 999;
}
}

where onChange="getValue(this.1)" in this case