Reputation:
I have a question regarding Javascript array.
I have the following javascript array:
var startTimeList= new Array();
I've put some values in it. Now I have the following input (hidden type):
<input type="hidden" value"startTimeList[0]" name="startTime1" />
Hoewever, this is obviously not correct because the javascript array is not recognized in the input hidden type. So I cant even get one value.
Does anyone know how I can get a value in the input type from a javascript array?
Upvotes: 0
Views: 3462
Reputation: 13275
You'd need to split the array into a delimited string and then assign that string to the value of the hidden input.
Then, on postback or similar events you'd want to parse the value back into an array for use in JavaScript:
var startTimeList = [1,2,3,4,5];
var splitList = '';
for(var i = 0; i < startTimeList.length; i++)
{
splitList += startTimeList[i] + '|';
}
and back again:
var splitList = '2|4|6|8|';
var startTimeList = splitList.split('|');
Upvotes: 0
Reputation: 382150
Use this :
<script>
window.onload = function() {
document.getElementsByName("startTime1")[0].value = startTimeList[0];
}
</script>
Upvotes: 1
Reputation: 13597
You can find your element by name with:
document.getElementsByName(name)[index].value = 'new value';
OR
You should identify your element and then change the value;
id="ex"
var element = document.getElementById('ex')
element.value = 'your value';
Upvotes: 0
Reputation: 5108
You need to set the value through JavaScript itself so.
document.getElementById("startTime1").value = startTimeList[0];
Or JQuery
$("#startTime1").val(startTimeList[0]);
Assign "startTime1" as the id above.
Upvotes: 0
Reputation: 887453
You need to set the value in Javascript:
document.getElementById(...).value = startTimeList[0];
Upvotes: 2
Reputation: 5272
You have to set value from javascript. Something like document.getElementById (ID).value = startTimeList[0];
You execute javascript from body oload event.
Upvotes: 0