Go4Expert

Go4Expert (http://www.go4expert.com/)
-   JavaScript and AJAX (http://www.go4expert.com/forums/javascript/)
-   -   Changing the hidden element's value in javascript (http://www.go4expert.com/forums/changing-hidden-elements-value-t8697/)

rag84dec 7Feb2008 09:29

Changing the hidden element's value in javascript
 
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'>


pradeep 7Feb2008 17:57

Re: Changing the hidden element's value in javascript
 
Code: JavaScript

var temp="name"+1;
// change the value now
document.forms[0].elements[temp].value = 'newvalue';


oogabooga 7Feb2008 21:08

Re: Changing the hidden element's value in javascript
 
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>



All times are GMT +5.5. The time now is 07:05.