I’m creating truth tables in a Pluto notebook in html, and want to use javascript/CSS to show result columns when one clicks on the headers, as in this jsfiddle example,
(Note the <th onclick="toggleColumn(2)">
calls to the javascript function toggleColumn(n)
.)
I can define this function in html"<script>...</script>"
in Pluto, but when I inspect the html, I find
<script>{
window.____FUNCTION_TO_RUN_INSIDE_SCRIPT.result = window.____FUNCTION_TO_RUN_INSIDE_SCRIPT.function_to_run(window.____FUNCTION_TO_RUN_INSIDE_SCRIPT.currentScript)
}</script>
and clicking fails to find toggleColumn(2). (Console error: Uncaught ReferenceError: toggleColumn is not defined at HTMLTableCellElement.onclick (edit?id=bf519f00-f7c8-11ec-00ac-937614ae1677:1:1)
)
So my fundamental confusion is what is allowed to be inside the <script>
tags for html in Pluto? And where can I find the documentation for that?/
Javascript in Pluto examples I’ve seen all define eventListeners in javascript (rather than html) and use anonymous functions. I think I could do this with a new function for each column, but I am eager to learn how I should think about this to find a better way.
Omitting the Julia to generate the truth table html, how would I get the following html to toggle column visibility when clicking on the column headers, as in the jsfiddle?
Here is a minimal non-working attempt to do this in a Pluto cell using HypertextLiteral.
using HypertextLiteral
htl"""
<div>
<script>
function toggleColumn(n) {
var div = currentScript.parentElement
var TT = div.querySelector(".truthtable")
var currentClass = TT.className;
if (currentClass.includes("c"+n+"hide"))
TT.className = currentClass.replace("c"+n+"hide", "").trim();
else
TT.className += " c"+n+"hide";
}
</script>
<table class="truthtable c1hide c2hide c3hide c4hide"><thead><tr>
<td onclick="() => (toggleColumn(0);)"><label>p</label></td><td onclick="toggleColumn(0);"><label>q</label></td><th onclick="toggleColumn(1);"><label>p → q</label></th><th onclick="toggleColumn(2);"><label>¬q → ¬p</label></th><th onclick="toggleColumn(3);"><label>q → p</label></th><th onclick="toggleColumn(4);"><label>¬p → ¬q</label></th></tr></thead><tbody><tr><td class="col0" style="background-color:#EEFFEE">T</td><td class="col0" style="background-color:#EEFFEE">T</td><th class="col1" style="background-color:#EEFFEE">T</th><th class="col2" style="background-color:#EEFFEE">T</th><th class="col3" style="background-color:#EEFFEE">T</th><th class="col4" style="background-color:#EEFFEE">T</th></tr><tr><td class="col0" style="background-color:#EEFFEE">T</td><td class="col0">F</td><th class="col1">F</th><th class="col2">F</th><th class="col3" style="background-color:#EEFFEE">T</th><th class="col4" style="background-color:#EEFFEE">T</th></tr><tr><td class="col0">F</td><td class="col0" style="background-color:#EEFFEE">T</td><th class="col1" style="background-color:#EEFFEE">T</th><th class="col2" style="background-color:#EEFFEE">T</th><th class="col3">F</th><th class="col4">F</th></tr><tr><td class="col0">F</td><td class="col0">F</td><th class="col1" style="background-color:#EEFFEE">T</th><th class="col2" style="background-color:#EEFFEE">T</th><th class="col3" style="background-color:#EEFFEE">T</th><th class="col4" style="background-color:#EEFFEE">T</th></tr></tbody></table>
<style>
table.c0hide .col0 { visibility: hidden; }
table.c1hide .col1 { visibility: hidden; }
table.c2hide .col2 { visibility: hidden; }
table.c3hide .col3 { visibility: hidden; }
table.c4hide .col4 { visibility: hidden; }
label:hover {
outline: thin solid;
cursor: cell;
}
</style>
</div>
"""