Javascript push User Guide and push User Guide
The push () method can add one or more elements to the end of an array and return a new length. The returned value is the new length after the specified value is added to the array.
Syntax: arrayObject. push (newelement1, newelement2,..., newelementX)
Required. The first element to be added to the array.
Optional. The parameter newelement2. The second element to be added to the array.
Optional. newelementX. You can add multiple elements.
The push () method can add its Parameter order to the end of arrayObject. It directly modifies the arrayObject instead of creating a new array. The push () and pop () methods use the advanced post-stack functions provided by arrays. This method changes the length of the array.
Example:
Copy codeThe Code 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 = "the number you 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 () to the arrays array.
}
}
Alert (arrays. length)
}
</Script>
</Body>