www.webdeveloper.com
Results 1 to 2 of 2

Thread: How to create an array of arrays at runtime

  1. #1
    Join Date
    Mar 2014
    Posts
    1

    How to create an array of arrays at runtime

    Hi.. In javascript, I need to create an array of arrays at runtime.
    I have a list of key values and every value must contains a list of field names.. example:
    to the first array I need to add new code "cod1" that is a list itself where I have to add "field1" ,"field2" ecc..
    so.. at runtime execution I need to add dynamically codes and fields too..
    how can I do that, please?

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,656
    something like this

    Code:
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8" />
    <title>Title</title>
    <style>
    body{padding-top:200px;}
    body,input{text-align:center;}
    #res{margin-top:15px;height:250px;width:300px;overflow:auto;display:inline-block;}
    </style>
    <script>
    var mycoll={};
    
    function $(id){return document.getElementById(id);}
    
    function addline(){
    var a=arguments[0],
        b=arguments[1],
    	c=Number(arguments[2]);
    if(mycoll[a]!==undefined){alert('this name already exists! pick another one');return;}
    else{
    var temp=[];
    for(var i=0; i<c; i++){temp.push(b+i);}
    mycoll[a]=temp;
    showall();
    }
    }
    
    function showall(){
    var output='';
    for(var i in mycoll){output+='<a href="#null" title="click me" onclick="javascript:var str=mycoll[\''+i+'\']+\'   \'+typeof mycoll[\''+i+'\'];alert(str);">'+i+'</a> => '+mycoll[i]+'<br />';}
    $('res').innerHTML=output;
    }
    
    window.onload=function(){
    $('go').onclick=function(){
    var n=$('name').value||false,
        f=$('fieldname').value||false,
    	h=$('howmany').value||false,
    	x=/[\d]{1,}/g.test(h);
    if(n&&f&&h&&x){addline(n,f,h);}
    else{
    $('res').innerHTML='all the fields must be <span style="color:Crimson">correctly</span> filled';
    if(!x){$('howmany').value='';}
    return;
    }
    }
    setTimeout(function(){$('name').focus();},1000);
    }
    </script>
    </head>
    <body>
    <input id="name" placeholder="letters and numbers" type="text" />
    <input id="fieldname" placeholder="letters and numbers" type="text" />
    <input id="howmany" placeholder="only numbers" type="text" />
    <input id="go" type="button" value="Add" />
    <br />
    <div id="res"></div>
    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles