0

I am down on this. i cant figure out how i can use a variable argument passed to a function to access a DOM element.

here is what i was trying to do

function resetField(fieldName){
 document.forms[1].fieldName.options.length = null;
    var option = new Option();
     option.value = "";
     option.text = "--Select a "+fieldName.toUpperCase()+"--";
     document.forms[1].level.options[0] = option;
 }

Call the above function like so

resetField('course');

the problem is in the function argument "fieldName" javascript says:

"document.forms[1].fieldName is undefined "

javascript is trying to read the fieldName variable as if it is not a variable because from firebug when you hover over the it, shows its == to the string in the function call but inside the function it says its undefined.

what can i over come this problem thank you.

katwekibs
  • 1,342
  • 14
  • 17
  • 3
    `document.forms[1][fieldName]`? – putvande May 20 '14 at 13:57
  • why are you setting the length to null? `document.forms[1].fieldName.options.length = null;` – Wez May 20 '14 at 14:01
  • @wezly the field gets its options dinamically when a user selects another field. say a user selcets computer courses in another field, then that field populates all available computer courses. but if the user decided to change the option in the category filed computer courses, then this field' generated options should also be removed. that why i use the option lenth null. – katwekibs May 20 '14 at 14:13

1 Answers1

0

You can use document.forms[1][fieldName]

putvande
  • 15,068
  • 3
  • 34
  • 50