Hi Im new to js and i need a function to pass values from selected options in a dropdown to hidden fields. This worked for one hidden value but what if i have 8 hidden values: function onChange() { var Current = document.form.procedure.selectedIndex; document.forms["form"].elements["00N200000013PAd"].value = document.form.procedure.options[Current].value; } I need a function like: when option1 selected pass the value to checkbox1 when option2 selected pass the value to checkbox2 i tried something like this but didnt work function onChange() { var Current = document.form.procedure.selectedIndex; if (document.getElementById="00N200000013PAd") { document.forms["form"].elements["00N200000013PAd"].value = document.form.procedure.options[Current].value; } else if (document.getElementById="00N200000013PAx") { document.forms["form"].elements["00N200000013PAx"].value = document.forum.procedure.options[Current].value; } . . . Code (markup): Thanks for any help
Try something like this : function onChange() { var Current = document.form.procedure.selectedIndex; if (Current==0) { document.forms["form"].elements["00N200000013PAd"].value = document.form.procedure.options[Current].value; } else if (Current==1) { document.forms["form"].elements["00N200000013PAx"].value = document.forum.procedure.options[Current].value; } . . . Code (markup): It can be much easier to do with arrays, if it works tell me, if not i will do for you an example.
Thanks for your reply this worked but lets say i chose option 1 and then i changed my mind and chose option 2 the output now is both options has been passed a value
Define default values of your checkboxes, for example. if (Current==0) { document.forms["form"].elements["00N200000013PAd"].value = document.form.procedure.options[Current].value; // set default values for others. } else if (Current==1) { document.forms["form"].elements["00N200000013PAx"].value = document.forum.procedure.options[Current].value; } Code (markup): I think, your checkboxes're have the same styles (CSS). Get all checkboxes by getElementsByClassName("CSS_STYLE_NAME"); If u have, for example 100 checkboxes, your code will be too big: if (Current==0) { document.forms["form"].elements["00N200000013PAd"].value = document.form.procedure.options[Current].value; // set default values for others. document.forms["form"].elements["00N200000013PAd_1"].value = default["00N200000013PAd_1"]; document.forms["form"].elements["00N200000013PAd_2"].value = default["00N200000013PAd_2"]; document.forms["form"].elements["00N200000013PAd_3"].value = default["00N200000013PAd_3"]; ............ ............ ............ document.forms["form"].elements["00N200000013PAd_98"].value = default["00N200000013PAd_98"]; document.forms["form"].elements["00N200000013PAd_99"].value = default["00N200000013PAd_99"]; document.forms["form"].elements["00N200000013PAd_100"].value = default["00N200000013PAd_100"]; } Code (markup): document.getElementsByClassName definition: document.getElementsByClassName = function(cl) { var retnode = []; var myclass = new RegExp('\\b'+cl+'\\b'); var elem = this.getElementsByTagName('*'); for (var i = 0; i < elem.length; i++) { var classes = elem[i].className; if (myclass.test(classes)) retnode.push(elem[i]); } return retnode; }; Code (markup): For example, u have 100 checkboxes with class="chk". 100 options to select difined 100 default values in default[] massive (numeration starts from [1]) var chkboxes = documentGetElementsByClassName('chk'); var default = new Array(1 => "default 1", ..., 100 => "default 100"); if (Current==0) { document.forms["form"].elements["00N200000013PAd"].value = document.form.procedure.options[Current].value; // set default values for others. for (i = 1; i <= 100; i++) { if (i-1 != 0) chkboxes[i] = default[i]; } } Code (markup):