Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

82
Vistas
New htm table row with editable cells

im creating an html table that starts with only a header row and the user has an add new row button that adds rows to the table. Ive managed to get that to work but I need the new row to be editable by the user. I tried using contenteditable=“true” but i can only do that if the rows were already created in the html file. How do i make my javascript function that creates the new cells include the contenteditable property?

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can set the attribute of an element after you created it & before you actually add it to the DOM.

const getCellEditable = () => {
  const cell = document.createElement("td")
  cell.setAttribute("contenteditable", true)
  return cell
}

const getRow = () => {
  const row = document.createElement("tr")
  row.append(getCellEditable())
  row.append(getCellEditable())
  return row
}

const tbody = document.getElementById("tbody")
const btnAddRow = document.getElementById("add-row")

const addRow = (parent) => {
  parent.append(getRow())
}

btnAddRow.addEventListener("click", function() {
  addRow(tbody)
})
tr,
th,
td {
  border: 1px solid black;
}

#table {
  border-collapse: collapse;
}

th,
td {
  padding: 8px 16px;
}
<button id="add-row">ADD ROW +</button>
<br />
<br />
<table id="table">
  <thead>
    <tr>
      <th>
        First
      </th>
      <th>
        Second
      </th>
    </tr>
  </thead>
  <tbody id="tbody">
  </tbody>
</table>

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda