Changing the hidden element's value in javascript

rag84dec's Avatar, Join Date: Jul 2007
Go4Expert Member
Hi,
i want to change the hidden elemtn value in the javascript.
the name of the filed will have to be formed in the javascript.

like this
Code:
var temp="name"+1;

document.temp.value="newname";

i want temp to be replaced by name1 so that the value
 of name1 field shud change.like this "document.name1.value="newvalue"  "



Code:
<input type='hidden' name='name1' value'Val'>

Last edited by rag84dec; 7Feb2008 at 09:29.. Reason: margin set
0
pradeep's Avatar, Join Date: Apr 2005
Team Leader
Code: JavaScript
var temp="name"+1;
// change the value now
document.forms[0].elements[temp].value = 'newvalue';
0
oogabooga's Avatar
Ambitious contributor
If you want to change the "value" (as you say) of an element named "name1" presumably you just do this:
Code:
name1.value = 'newvalue'
...
<input type="hidden" name="name1" value="oldvalue">
If you are trying to programmatically change from name1 to name2 etc., then you want eval, which allows you to execute a string as code.
Code:
<script>
function changeit() {
   var newval = "xxxxxxxxxxxxx"
   for (var i = 1; i <= 5; ++i)
      eval( 'name' + i + '.innerText="' + newval + '"' )
}
</script>

<body>
<p id=name1>Paragraph 1</p>
<p id=name2>Paragraph 2</p>
<p id=name3>Paragraph 3</p>
<p id=name4>Paragraph 4</p>
<p id=name5>Paragraph 5</p>
<button onclick=changeit()>Press</button>
</body>