Setup: On each line I a dropdown list of Inventory Part ids (with the name showing). On the same line I have a lot of hidden fields. Some of these fields need to reflect the selected <OPTION>. The fields which need to update are the ones with an id of "gps[X][Y]_part_id" where X = the line number (in this case it's '1'), and Y=anything.
Details:
My dropdown html:
<select ONCHANGE="set_part_id(this.form,this,1)" id="gps[1]_part_id" name="gps[1][part_id]">
<option value="">- Select -</option>
<option value="10">NS-BV49ZD</option>
<option value="2">NS-PVC10</option>
<option value="3">NS-2inchpipe</option>
</select>
My Hidden fields:
<td><input id="gps[1][U]_num_qty" name="gps[1][RU][num_qty]" size="5" type="text" /></td>
<input id="gps[1][U]_id" name="gps[1][U][id]" type="hidden" />
<input id="gps[1][U]_group_id" name="gps[1][U][group_id]" type="hidden" value="14" />
<input id="gps[1][U]_part_id" name="gps[1][U][part_id]" type="hidden" />
<input id="gps[1][U]_phase_id" name="gps[1][U][phase_id]" type="hidden" value="1" />
<input id="gps[1][U]_unit_id" name="gps[1][U][unit_id]" type="hidden" value="1" />
<input id="gps[1][U]_line_id" name="gps[1][U][line_id]" type="hidden" value="1" />
<td><input id="gps[1][RU]_num_qty" name="gps[1][RU][num_qty]" size="5" type="text" /></td>
<input id="gps[1][RU]_id" name="gps[1][RU][id]" type="hidden" />
<input id="gps[1][RU]_group_id" name="gps[1][RU][group_id]" type="hidden" value="14" />
<input id="gps[1][RU]_part_id" name="gps[1][RU][part_id]" type="hidden" />
<input id="gps[1][RU]_phase_id" name="gps[1][RU][phase_id]" type="hidden" value="2" />
<input id="gps[1][RU]_unit_id" name="gps[1][RU][unit_id]" type="hidden" value="1" />
<input id="gps[1][RU]_line_id" name="gps[1][RU][line_id]" type="hidden" value="1" />
<td><input id="gps[1][TO]_num_qty" name="gps[1][TO][num_qty]" size="5" type="text" /></td>
<input id="gps[1][TO]_id" name="gps[1][TO][id]" type="hidden" />
<input id="gps[1][TO]_group_id" name="gps[1][TO][group_id]" type="hidden" value="14" />
<input id="gps[1][TO]_part_id" name="gps[1][TO][part_id]" type="hidden" />
<input id="gps[1][TO]_phase_id" name="gps[1][TO][phase_id]" type="hidden" value="4" />
<input id="gps[1][TO]_unit_id" name="gps[1][TO][unit_id]" type="hidden" value="1" />
<input id="gps[1][TO]_line_id" name="gps[1][TO][line_id]" type="hidden" value="1" />
<td><input id="gps[1][TR]_num_qty" name="gps[1][TR][num_qty]" size="5" type="text" /></td>
<input id="gps[1][TR]_id" name="gps[1][TR][id]" type="hidden" />
<input id="gps[1][TR]_group_id" name="gps[1][TR][group_id]" type="hidden" value="14" />
<input id="gps[1][TR]_part_id" name="gps[1][TR][part_id]" type="hidden" />
<input id="gps[1][TR]_phase_id" name="gps[1][TR][phase_id]" type="hidden" value="5" />
<input id="gps[1][TR]_unit_id" name="gps[1][TR][unit_id]" type="hidden" value="1" />
<input id="gps[1][TR]_line_id" name="gps[1][TR][line_id]" type="hidden" value="1" />
LOL, that's a ton of hidden fields, ain't it? Good thing this is all only on our internal network. The 2nd index (U/RU/TO/TR) (and thus the number of 'phases') is dynamically generated from another table.
I don't really know how to access the 'value' inside those hidden fields. Here's my start on a javascript function:
function set_part_id(form,obj,line_num) {
while (obj && obj.tagName != 'SELECT')
obj = obj.parentNode;
var n = obj.selectedIndex; // Which menu item is selected
var txt = obj[n].text; // Return string of selected item
var val = obj[n].value; // Return value of selected item
//alert('Value: ' + val + ', Name: ' + txt);
//alert('Field: ' + obj + ' Form: ' + form);
var x = form.elements[line_num].id;
alert(x.text); // is blank...
//for (var i=0;i<x.length;i++) {
// Now we should be looping through just the inputs that are hidden and have ids that end with "...part_id" on only this line (only this line_num)
// loop_item_value.value = val;
//}
}
Can someone help me write this javascript function? Thanks a ton!
I've modified my <select> starting to tag to this:
<select ONCHANGE="set_part_id(this.form,this,1)" id="gps_c_part_id" name="gps[c_part_id]">
And my function now looks like this;
function set_part_id(form,obj,line_num) {
while (obj && obj.tagName != 'SELECT')
obj = obj.parentNode;
var n = obj.selectedIndex; // Which item is selected
var txt = obj[n].text; // Return string of item
var val = obj[n].value; // Return value of item
var x = form;
for (var i=0;i<x.length;i++){
var str = x.elements[i].name;
if (str.match("[" + line_num + "[part_id]")) {
alert(str);
}
}
}
So I'm a bit further along, but for some reason it says that 'str' has no properties. So now I'm working on trying to fool javascript into thinking that str is a regular string, not some value that came from a form.
And here is my working function, for anyone who happens to stumble across this thread in the future.
function set_part_id(form,obj,line_num) {
while (obj && obj.tagName != 'SELECT')
obj = obj.parentNode;
var n = obj.selectedIndex; // Which item is selected
var txt = obj[n].text; // Return string of item
var val = obj[n].value; // Return value of item
var x = form;
var str = "";
var f1 = "\\[" + line_num + "\\]";
var f2 = "\\[part_id\\]";
for (var i=0;i<x.length;i++){
if(x.elements[i].name != null) {
str = x.elements[i].name;
t1 = str.match(f1);
t2 = str.match(f2);
if ( t1 != null && t2 != null ) {
x.elements[i].value = val;
}
}
}
}
1 to 3 of 3