Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Javascript-array JavaScript Array from Input

Related Tutorials


 
 

Share on Google+Share on Google+

JavaScript Array from Input

Advertisement
In this Tutorial we want to describe you a code that makes you easy to understand an example of Array from Input.

JavaScript Array from Input

     

In this Tutorial we want to describe you a code that makes you easy to understand an example of  Array from Input. The example create a HTML Page JavaScript array from input include a text name 'name', a text field and a button name inset into array. On click a button, a show( ) and insert ( ) function is invoked. The array variable instantiate an array object and hold its values in an array. The function insert ( )accept the value entered by the user. The function show ( ) includes string variable that hold all the element of the array. The for loop run and execute the script till the length i is less than array length. The variable string store the concatenated value of string and array.

The if operator return you the all the element of string in case the array length is more than zero.

JavaScript Array from Input.html  

<html>
  <head>
  <title> JavaScript Array from Input</title>
  <script>

  var array = new Array();

  function insert(val){
  array[array.length]=val;
  }

  function show() {
  var string="<b>All Element of the Array :</b><br>";
  for(i = 0; i < array.length; i++) {
  string =string+array[i]+"<br>";
  }
  if(array.length > 0)
 document.getElementById('myDiv').innerHTML = string;
  }
  </script>
  
  </head>
  
  <body>
  <h2>JavaScript Array from Input</h2>
  <form name="form1">
  <table width="407">
  <tr>
  <td width="154" align="right"><b>Name</b>
  <td width="9"><b>&nbsp;:</b>
  <td width="224">
  <input type="text" name="name"/>
  </tr>
  <tr>
  <td width="154" align="right">
  <td width="9">
  <td width="224">
  </tr>
  <tr>
  <td width="154" align="right">
  <td width="9">
  <td width="224">
  <input type="button" Value="Add Into Array" 
 onclick="insert(this.form.name.value),show();"/>
  </tr>
  </table>
  </form>
  <div id="myDiv"></div>
  </body>
</html>

Output

After insert value

Download Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 13, 2008

Related Tutorials

Discuss: JavaScript Array from Input  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
Madhuchandra
November 5, 2011
Excellent Example

The above example is short and sweet. It is an excellent example. Thanks a lot.
hady
May 7, 2013
value from one text input into array text input

help me to get value from one text input into array text input using javascript.. I found like this: <script language="javascript"> function kirim() { document.form1.data2.value=document.form1.data1.value } </script> <form action="" method="post" name="form1"> <input type="text" name="data1" onKeyUp="kirim()"/> <br /><br> <input type="text" name="data2"> </form> But not work if like this: <script language="javascript"> function kirim() { document.form1.data2.value=document.form1.data1.value } </script> <form action="" method="post" name="form1"> <input type="text" name="data1" onKeyUp="kirim()"/> <br /><br> <? foreach ($countries as $data2) { <input type="text" name="$data2['location'][]" id="good"> }
DMCA.com