Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

push input value to array javascript

var names = ['Riley', 'Takit', 'Maria', 'Oprah']
var input = document.querySelector('input');
var button = document.querySelector('button');

// Let's say the user types in a name like 'Emeka' in the input section

button.onclick = () => {
  var newName = input.value; // Captures the value of the input when the button is clicked
  names.push(newName);		 // This updates the existing names array adding the newName to the end of the array
  console.log(names);        // Result: ['Riley', 'Takit', 'Maria', 'Oprah', 'Emeka']
}

// Pen here 'https://codepen.io/emekaorji/pen/yLvNWaP'
// A better pen here 'https://codepen.io/emekaorji/pen/YzeXjvr'
 
PREVIOUS NEXT
Tagged: #push #input #array #javascript
ADD COMMENT
Topic
Name
6+1 =