I am using Office JS API to create an Office add-in, and wanted to capture checkbox events when selecting values. I call an API that returns a JSON object, with a key-value pair, where the value is an array of strings. I then display the object as a table.
I have a table tag:
<table id="out-table"></table>
..and a button:
<button class="ms-Button ms-Button--hero" id="sim">Process</button>
..which is triggered by:
document.getElementById("sim").onclick = process;
..and in my JS code (inside the process
function), I create the table rows and data values:
const table = document.getElementById("out-table");
proposals.forEach((pr, idx) => {
const sm = summaries[idx];
const row = document.createElement("tr");
const prElement = document.createElement("td");
const smElement = document.createElement("td");
prElement.style.fontSize = "9px";
prElement.style.fontWeight = "bold";
smElement.style.fontSize = "9px";
const prContent = document.createTextNode(pr);
const smContent = document.createTextNode(truncate(sm, 150));
const prSelect = document.createElement("input");
prSelect.type = "checkbox";
prSelect.id = "ss"; // does this make sense to have it the same ID?
prSelect.value = pr;
const label = document.createElement("label");
label.htmlFor = pr;
prElement.appendChild(prSelect);
prElement.appendChild(label);
prElement.appendChild(prContent);
smElement.appendChild(smContent);
row.appendChild(prElement);
row.appendChild(smElement);
table.appendChild(row);
});
..where proposals and summaries are arrays of strings. The input element created just adds a checkbox before the prContent with its label so it something like this:
A header | Another header |
---|---|
[x] V1 | row |
[ ] V2 | row |
When the table is populated, I want to multi-select the checkboxes then press a button that captures the values (V1, V2), and use them - log them for example. How would I do this? I want to be able to dynamically select and deselect and press a button that outputs only the selected values.