I want to use switch by using HTML buttons. I have 3
buttons, tried for now only with the first button. I gave it id=1
, and once clicked it should pass value 2
to function switchProduct()
which will use case:2
however, once the page loaded, the function is executing:
console.log('Apples are $0.32 a pound.') -
okay so I didn't even press the button, and it has passed value to switch once the page loaded, and when I pressing the button it's simply not working (it is not console logging anymore), it's just one time run function - how to fix it? I want to use buttons how many times I need.
script.js
let bttn = document.getElementById("1");
bttn.addEventListener("click", switchProduct(2));
function switchProduct(x)
{
switch (x)
{
case 1:
console.log('Oranges are $0.59 a pound.');
break;
case 2:
console.log('Apples are $0.32 a pound.');
break;
case 3:
console.log('Bananas are $0.48 a pound.');
break;
case 'Cherries':
console.log('Cherries are $3.00 a pound.');
break;
default:
console.log('Sorry, we are out of ');
}
}
index.html
<button id="1" class="inline-block typeU">1</button>