The push() method adds one or more elements to the end of the array and returns a new length. The return value is the new length after adding the specified value to the array.
Syntax: arrayObject.push(newelement1,newelement2,...,newelementX)
Parameter newelement1, required. The first element to be added to the array.
Parameter newelement2, optional. The second element to be added to the array.
Parameter newelementX, optional. Multiple elements can be added.
The push() method adds its argument order to the end of the arrayObject. It directly modifies the arrayObject instead of creating a new array. The push() method and the pop() method use the first-in-and-thread function provided by the array. This method changes the length of the array.
Example:
The code copy is as follows:
<!doctype html>
<meta charset="utf-8">
<body>
<input type="checkbox" value="1" name="check" checked="checked"/>
<input type="checkbox" value="1" name="check"/>
<input type="checkbox" value="1" name="check" checked="checked"/>
<input type="checkbox" value="1" name="check" />
<input type="button" value="number of selected" id="btn" />
<script>
var btn=document.getElementById('btn');
btn.onclick=function(){
var arrays=new Array();
var checkitem=document.getElementsByName("check");
for(var i=0;i<checkitem.length;i++)
{
if(checkitem[i].checked){
arrays.push(checkitem[i].value);//Pass the value in() into the array array
}
}
alert(arrays.length)
}
</script>
</body>