- Forums
- Javascipt
- How To Make Array In Javascript
Making Array In Javascript This Post Will Show You How You Can Make Arrays In Your Javascript Using Our Example Code And Will Explain How To Use The Javascript Code To Make An Array From The Fields In A Form Using Input Tags And Values Of Elements [2196], Last Updated: Mon Jun 24, 2024
Webune Support
Sun Dec 27, 2009
1 Comments
561 Visits
Welcome to Webune Web Hosting Support Forums.
We provide web hosting for many websites. many customer use javascript on their websites so we are going to show you today how you can create an array using javascript in your html.
If you are familiar with arrays, you know that each array element contains a value so for example, lets say that i have a web form and each input field creates an element on my array, so you can use this example code to see how it works:
JAVASCRIPT CODE:
<script type="text/javascript" language="javascript">
function submitted(len){
for ( var i=0; i<len; i++ ){
alert(document.getElementById(i).value);
}
}
</script>
Colors:
<form name='myform'>
first color: <input type="text" id="0" value="red"><br />
second color: <input type="text" id="1" value="blue"><br />
third color: <input type="text" id="2" value="green"><br /><br />
<input type="button" value="send" onclick="submitted(3);"/>
</form>
as you can see from the code above, i am using a for loop to display the values of each field by using a popup alert window.
try it:
Colors:
more advanced: if you feel confortable using this script, we can take it to the next level. as you can see in the code, the submit button has an onclick() which tells your function you have 3 elements in the submitted() function, but how about intead, let javascript determine how many elements there are in the array, visit our next tutorial to find out how:
Count Number Of Elements In An Array
https://www.webune.com/forums/how-to-make-array-in-javascript.html