I tried to implement the code example below inside a notebook here
<body>
<p>Click the button to display the date.</p>
<button onclick="displayDate()">The time is?</button>
<script>
function displayDate() {
document.getElementById("demo").innerHTML = Date();
}
</script>
<p id="demo"></p>
</body>
However, it seems using a function with html
and <button onclick="">
is not straightforward here.
html`<button onclick="displayDate()">The time is?</button>` // cell 1
I first tried cell 1 which uses a function (displayDate) inside quotation, nothing works.
html`<button onclick="${displayDate()}">The time is?</button>` // cell 2
Then I tried cell 2. This time the function can be executed, but clicking the button wonât actually execute the function.
I know there are other ways to click the button to execute the function, I just wonder:
- why exactly cell 1 wonât work
- why clicking the button based on cell 2 wonât execute the function
- are there easy solutions to make cell 1 and cell 2 work?
Thanks!