I've asked this question twice before ([1], [2]) and got some good answers, but none of them were quite what I was looking for.
The situation: I have a table, and the cells in one column are contentEditable
. The problem: The editable area does not reach all the edges of the cell. Instead, there is a thin padding (5px) of un-editable area underneath the editable section. I want that 5px of space to stay, but also want it to be editable (i.e. I like the dimensions, but not the un-editable aspects of it).
I am already aware that JavaScript allows for the document.getElementById("id").contentEditable = true;
property to do just this. In fact, I would much prefer to use this method (and if there is a way, please let me know)! However, I am purposely trying to set up an un-editable unit on one end of the cell using <span>
, which ends up getting taken up as input by the script when I need the cells to do a function (e.g. when adding a sum of numbers, the unit gets parsed in with the integers, and the output becomes NaN
).
Visually, what I'm looking for is as shown in this mock-up:
Below is a visual representation of the unnecessary padding (except on the right side), shown in red (I changed the color in the CSS just to make it visible here). To be clear, I still like the dimensions of the cells, but I want to make the entire area of the cell editable, except for where the text "kg" is. Also, the red area to the right of "kg" isn't unnecessary padding, it should remain un-editable.
Additionally, here is the code I have so far:
function myFunction() {
var jack = document.getElementById("jack").innerText;
var john = document.getElementById("john").innerText;
var joe = document.getElementById("joe").innerText;
var total = parseInt(jack) + parseInt(john) + parseInt(joe);
document.getElementById("total").innerHTML = total;
}
myFunction();
* {
box-sizing: border-box;
}
table {
width: 100%;
}
table,
tr,
th,
td {
background-color: #fff;
border: 1px solid black;
border-collapse: collapse;
font-family: Arial, sans-serif;
}
td:last-child {
display: flex;
justify-content: space-between;
align-items: center;
border: none;
}
td:last-child,
span:first-child {
flex-grow: 1;
margin-right: 10px;
outline: none;
text-align: right;
}
.cell {
height: 30px;
}
#total {
display: flex;
justify-content: flex-end;
}
<table>
<thead>
<tr class="cell">
<th>Person</th>
<th>Weight</th>
</tr>
</thead>
<tbody>
<tr class="cell">
<td>Jack</td>
<td id="jack" oninput="myFunction()">
<span contentEditable="true">4000</span>
<span>kg</span>
</td>
</tr>
<tr class="cell">
<td>John</td>
<td id="john" oninput="myFunction()">
<span contentEditable="true">200</span>
<span>kg</span>
</td>
</tr>
<tr class="cell">
<td>Joe</td>
<td id="joe" oninput="myFunction()">
<span contentEditable="true">3</span>
<span>kg</span>
</td>
</tr>
<tr class="cell">
<td>Total</td>
<td>
<span id="total"></span>
<span>kg</span>
</td>
</tr>
</tbody>
</table>
As you can see, on the very corners/edges of the cell on the right column, there is about 5px of un-editable space on the top, bottom, and left side. Is there a way to make this specific part editable without a function receiving the units on the right side of the cell (where the unit is)?
Any help is much appreciated!