0

I know how to remove element of array by pop() method & splice() method, for example

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.pop();
document.getElementById("demo").innerHTML = fruits;

my question is how do I do these actions through input element in html? for example by create a button to toggle hide or show element of array so I want to toggle between remove "Mango" for example and add it again?

Shareef
  • 23
  • 4
  • Possible duplicate of [How to show and hide an element, pass values from javascript to html element using JavaScript (not jQuery)?](https://stackoverflow.com/questions/5986609/how-to-show-and-hide-an-element-pass-values-from-javascript-to-html-element-usi) – Muhammad Faizan Jul 11 '19 at 11:01
  • i.e. Once button is click, one element from the array is pop-out and again need to display the fruits. Is it like that? – Shivani Sonagara Jul 11 '19 at 11:04
  • any event you do the following things, click, mouseover,keypress, basically any event should trigger the function. – Vinod kumar G Jul 11 '19 at 11:07
  • This might help you https://stackoverflow.com/questions/3954438/how-to-remove-item-from-array-by-value – Ravi Jul 12 '19 at 04:38

1 Answers1

0

Use addEventListener

Example, where el is your button or whatever, and doSomething is the function you want to be executed on click:

var el = document.getElementById('some-id'); 
function doSomething() {
    console.log('a');
}
el.addEventListener("click", doSomething, false);

https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener

Snackoverflow
  • 600
  • 9
  • 24
  • I am not quite sure how do I remove the "Mango" in the example above and reuse it again similar to toggle – Shareef Jul 12 '19 at 04:11