JavaScript JavaScript form value with variable field name javascript form value by name,javascript form value set,javascript

simple javascript but can't seem to get it to work.

var number = 5;
var netiteration = "net"+number;  // makes netiteration now equal net5

var formvalue = document.forms.myformname.netiteration.value;

why doesn't this get the value of the form field with the name/id of "net5", in the form "myformname"?

also, I'm working from a 10 year old javascript book, so maybe the syntax has changed?

thanks

Answer:1

Try:

var number = 5;
var netiteration = "net"+number;  // makes netiteration now equal net5

var formvalue = document.forms.myformname[netiteration].value;

Your original code was looking for a field called "netiteration" but you want the field that has a name equal to the evaluated value of netiteration.

Answer:2

This will not work as netiteration is a variable and you cannot use variable name within the DOM structure of the HTML.

something like following should work....

var form_elements = document.forms.myformname.getAllChildren();
var net_elements = new Array[10];
for(var i=0;i<form_elements.length;i++)
{
   var name = form_elements[i].name;
   if(name.indexOf('net') != -1)
    net_elements[i] = form_elements[i];
}

now, the arraynet_elements has all the elements with the name 'net' in it...

hopes this solves your problem...

Answer:3

I've tried to make the Material Design Slide Indicator (watch image) with javascript. I use Ionic to control the slides (to "swipe", to set slide or to obtain the current slide) but the prolem doesn'...

I've tried to make the Material Design Slide Indicator (watch image) with javascript. I use Ionic to control the slides (to "swipe", to set slide or to obtain the current slide) but the prolem doesn'...

Hello (yet again) a question about javascript, this one has to be an easy one but I just can't see what I did wrong, I think something really stupid: I have a textbox and a button. When i click the ...

Hello (yet again) a question about javascript, this one has to be an easy one but I just can't see what I did wrong, I think something really stupid: I have a textbox and a button. When i click the ...

var prompt = ... var connection = ... prompt.start(); var property = { name: 'yesno', message: 'approve this screencast?', validator: /y[es]*|n[o]?/, warning: 'Must respond yes or no', ...

var prompt = ... var connection = ... prompt.start(); var property = { name: 'yesno', message: 'approve this screencast?', validator: /y[es]*|n[o]?/, warning: 'Must respond yes or no', ...

I converted my CoffeeScript code to JavaScript using http://js2coffee.org ResetControls = -> $("#menu_item_url").val "" $("#menu_item_name").val "" $("#resource_id").prop "selectedIndex", 0 ...

I converted my CoffeeScript code to JavaScript using http://js2coffee.org ResetControls = -> $("#menu_item_url").val "" $("#menu_item_name").val "" $("#resource_id").prop "selectedIndex", 0 ...

  1. javascript function assigned to variable
  2. javascript call function assigned to variable
  3. javascript assign function with parameters