View Full Version : is there a way in javascript to create dynamic arrays
b_j_mall
08-30-2002, 01:18 AM
Hi everyone,
i am stuck at a problem.
i am trying to craete a redimmensionable array using javascript, is there a way to do this, i know that this can be done in vbscript using redim and preserve commands.
If not then is it possible to variables between javascript and vbscript, or call vbscript function using javascript.
a sample code will be much aprreciated
thanks for your time!
J
Arrays in Javascript grow as required.
var bla = new Array(3);
bla.length == 3 //true
bla[100] = 'something';
bla.length == 101 //true
glenngv
08-30-2002, 02:38 AM
so to add an item at the end of the array:
blah[blah.length] = 'something';
Shyama_Mukherji
09-18-2009, 11:29 AM
This is a script to create dynamic arrays in Javascript
<HTML>
<head>
<Script language = "Javascript">
//Example function to create dynamic arrays
function dynamicarr()
{
var i=0
arr = new Array(3)
alert('Hello')
arr[0]="One"
arr[1]="Two"
arr[2]="Three"
document.write(arr[0]+' '+arr[1]+' '+arr[2])
var num=3
// num is the number of items to be added
//Following is the modified declaration of the array to increase number of elements by num
arr = new Array(arr.length+num)
arr[3]="Four"
document.write(arr[3])
}
</Script>
</head>
<A href = "Javascript:dynamicarr()">trythis </A>
</HTML>
jmrker
09-18-2009, 02:48 PM
I'm not sure why you are calling it 'dynamic' array if you hard code it from the start (?)
Consider this:
<html>
<head>
<title>Dynamic Arrays</title>
<script type="text/javascript">
var dynArray = [];
function InsertEntry() {
var tmp = document.getElementById('entry').value;
dynArray.push(tmp);
}
function ShowEntries() {
document.getElementById('EntryList').innerHTML = dynArray.join('<br>');
}
</script>
</head>
<body>
Enter variable: <input id="entry" value="">
<button onclick="InsertEntry()">Append value</button>
<button onclick="ShowEntries()">Display values</button>
<div id="EntryList'></div>
</body>
</html>
Coded on the fly, so untested at this time ... should work ;)
:)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.