Home > Mobile >  Select text inside html table cell upon tabbing into the cell
Select text inside html table cell upon tabbing into the cell

Time:12-20

I feel silly for asking this, it seems like it should be very simple, and yet after several hours - here I am.

I have an HTML Table with <td> cells inside. Each cell has a default of "0". I'm trying to figure out how to select the cell contents upon tabbing and/or clicking into the cell. I've tried using CSS (i.e,. user-select: all; user-select: text) and the best I can do with javascript is to clear the cell on click:

document.getElementById(tableId).addEventListener('click', (e) => {
     if (!e.target.contentEditable === "true") return; //using this to register clicks on a cell
     e.target.innerText = ""
 })

I'd really like to just select the contents - not clear them - so the user can leave the value there and tab to the next one if they want to keep the default value. I've tried using "focus" events and the like, to no avail. How do I just select the text inside?

Any help is appreciated! Thanks!

CodePudding user response:

"Try using onfocus" (on the cells in the table)

  • Related