- Joined
- Sep 12, 2022
- Messages
- 39
- Reaction score
- 0
I have successfully executed Javascript functions without parameters, to display on my web browser but I have a problem outputting functions with parameter arguments. What's the correct way of outputting this function with a parameter argument to the web browser?
Here's the initial code, it outputs on the console of online code editors. It has item as the set parameter argument.
My attempt 1st at the output on a web browser with item still set as the argument. The issue with this attempt is that it doesn't wait for the onClick() before it displays the output
My attempt 2nd at the output on a web browser with item still set as the argument. This displays no output.
None of the attempts work. What's the correct method of output on a web browser? NOTE that I don't want to declare a variable stating that item = 4. Rather I want the value to be passed in and continue to have item, declared as a parameter of the doubler function
Here's the initial code, it outputs on the console of online code editors. It has item as the set parameter argument.
JavaScript:
const doubler = (item) => item * 2;
doubler(4);
My attempt 1st at the output on a web browser with item still set as the argument. The issue with this attempt is that it doesn't wait for the onClick() before it displays the output
JavaScript:
const doubler = (item) => item * 2;
document.getElementById("funcWithPara").innerHTML = doubler(4);
HTML:
<button onclick="doubler()">click me</button>
<p id="funcWithPara"></p>
My attempt 2nd at the output on a web browser with item still set as the argument. This displays no output.
JavaScript:
document.getElementById("myBtn").addEventListener("click", doubler())
const doubler = (item) => item * 2;
document.getElementById("funcWithPara").innerHTML = doubler(4);
HTML:
<button id="myBtn">click me</button>
<p id="funcWithPara"></p>
None of the attempts work. What's the correct method of output on a web browser? NOTE that I don't want to declare a variable stating that item = 4. Rather I want the value to be passed in and continue to have item, declared as a parameter of the doubler function