Accessing hidden element value (javascript)
Pls have a look at the following code.
<script type='text/javascript'>
function apps(){
var app= new Array(8);
for (var i=0;i<8;i++)
{
app[i]= ....;
}
}
</script>
<input type="hidden" name="NEW" value= ? >
< ....button label="Submit" OnClick='apps();return false;'/>
Here the apps() method gets 开发者_开发百科executed on clicking the Submit button. I want to access the value of app (Array) by use of a hidden element. Pls let me know what code I should write for this purpose.
<input type="hidden" name="foo" value="bar" />
document.write(document.getElementsByName('foo')[0].value);
output is "bar". getElementsByName returns an array of matching form elements with the name supplied. the [0]
grabs the first match, and .value
retrieves the value.
You can do this with JQuery as well.
<input id="foo-hidden" type="hidden" name="foo" value="bar" />
<script type="text/javascript">
function apps() {
var app= new Array(8);
for (var i=0; i < 8; i++) {
app[i]= $('#foo-hidden').val();
}
}
</script>
精彩评论