Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting input box values into an array in Javascript/HTML
    primarykey
    data
    text
    <p>I'm learning Javascript for a project. I'm trying to try and get values from an dynamic number of text inputs into a multi-dimensional array. I have tried this with 1 text field and it correctly saves into the array for further manipulation later. However when i added an second text field and another dimension to the array, it broke. Eventually i will have a drop down list to give the number of inputs once i figure it out. Here's my current code, not very efficient really. </p> <pre><code> &lt;html&gt; &lt;head&gt; &lt;title&gt; JavaScript Array from Input&lt;/title&gt; &lt;script&gt; var array = new Array(); function insert(val){ array[0][array.length]=val; } function insert2(val){ array[array.length][0]=val; } function show() { var string="&lt;b&gt;All Element of the Array :&lt;/b&gt;&lt;br&gt;"; for(i = 0; i &lt; array.length; i++) { string =string+array[i][0]+"&lt;br&gt;"; string =string+array[0][i]+"&lt;br&gt;"; } if(array.length &gt; 0) document.getElementById('myDiv').innerHTML = string; } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;h2&gt;JavaScript Array from Input&lt;/h2&gt; &lt;form name="form1"&gt; &lt;table width="407"&gt; &lt;tr&gt; &lt;td width="154" align="right"&gt;&lt;b&gt;Name&lt;/b&gt; &lt;td width="9"&gt;&lt;b&gt;&amp;nbsp;:&lt;/b&gt; &lt;td width="224"&gt; &lt;input type="integer" name="name"/&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td width="154" align="right"&gt; &lt;td width="9"&gt; &lt;td width="224"&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td width="154" align="right"&gt; &lt;td width="9"&gt; &lt;td width="224"&gt; &lt;td width="154" align="right"&gt;&lt;b&gt;Name2&lt;/b&gt; &lt;td width="9"&gt;&lt;b&gt;&amp;nbsp;:&lt;/b&gt; &lt;td width="224"&gt; &lt;input type="integer" name="name2"/&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td width="154" align="right"&gt; &lt;td width="9"&gt; &lt;td width="224"&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td width="154" align="right"&gt; &lt;td width="9"&gt; &lt;td width="224"&gt; &lt;input type="button" Value="Add Into Array" onclick="insert(this.form.name.value), insert2(this.form.name2.value);"/&gt; &lt;input type="button" Value="alert" onclick="alert(array[0][0])"/&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; &lt;div id="myDiv"&gt;&lt;/div&gt; &lt;/body&gt; </code></pre> <p></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload