I'm finding DOM manipulation tricky. I have written a function that uses the filter method on an array with numbers that are divisible by two.
The function evenNums works. I want to use the push() method to push numbers from the input text field to the array and display the result from the function evenNums on the page.
HTML
<!DOCTYPE html>
<html>
<head> </head>
<body>
<label for="text">Enter Text</label>
<input id="myText" type="text" />
<input type="radio" />
<label for="function">Function 1</label>
<input type="radio" />
<label for="function">Function 2</label>
<button id="btn">Click Me</button>
<div id="result"></div>
<script src="array.js"></script>
</body>
</html>
ignore the radio buttons. This is my Javascript
const inputBtn = document.getElementById("btn")
const myText = document.getElementById("myText")
const result = document.getElementById("result")
//function 1 filter method on array.
const nums = [1,2,3];
const answer = nums.filter(evenNums);
function evenNums(nums) {
if (nums%2 == 0) {
return nums
}
}
result.innerHTML = answer;
console.log(nums) // original array
console.log(answer) // array using filter method
//How do I push a value from the user to the array?
inputBtn.addEventListener ("click", function() {
nums.push(myText.value);
myText.value ='';
evenNums(nums);
});
This is how far I've got. How do I push a value from the user to the array and display it on the page? Am I close?
Thanks for all your help!