Javascript's push usage guide

  • 2020-03-30 04:29:39
  • OfStack

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.
Grammar: arrayObject. Push (newelement1, newelement2,... , newelementX)
Parameter newelement1, required. The first element to add to the array.
Parameter newelement2, optional. To add to the second element of the array.
Parameter newelementX, optional. Multiple elements can be added.

The push() method adds its argument order to the end of the arrayObject. Instead of creating a new array, it simply modifies the arrayObject. The push() and pop() methods use the advanced post-stack functionality provided by arrays. This method changes the length of the array.

Example:


<!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 () into the arrays array
             }
         }
         alert(arrays.length)
       }
    </script>
    </body>


Related articles: