user1319203
user1319203

Reputation:

Show javascript array value in input type hidden

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

Answers (6)

Widor
Widor

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

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382150

Use this :

<script>
window.onload = function() {
    document.getElementsByName("startTime1")[0].value = startTimeList[0];
}
</script>

Upvotes: 1

lukas.pukenis
lukas.pukenis

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;

  1. Give your element an ID for example id="ex"
  2. Get the element with JavaScript(of course once the DOM is ready) with var element = document.getElementById('ex')
  3. Change the value with element.value = 'your value';

Upvotes: 0

verisimilitude
verisimilitude

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

SLaks
SLaks

Reputation: 887453

You need to set the value in Javascript:

document.getElementById(...).value = startTimeList[0];

Upvotes: 2

elrado
elrado

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

Related Questions