when I write something in td and press enter key I get a new line. In the console I get whatever I have written + br tag. How can I fix it?
document.querySelector(".add_btn").addEventListener("click", () => {
const table = `<tr>
<td contenteditable="true" class="fill items"></td>
<td contenteditable="true" class="fill quan"></td>
<td contenteditable="true" class="fill rate"></td>
</tr>`;
document
.querySelector(".table_heading")
.insertAdjacentHTML("afterend", table);
const fill = document.querySelectorAll(".fill");
fill.forEach((f) => {
f.addEventListener("keyup", (e) => {
if (e.key == "Enter") {
console.log(f.innerHTML);
}
});
});
});```
There are two fixes required for your problem.
Enterkey, which is to insert a line break<br>in acontenteditableelement. Calle.preventDefault()in the event listener when the Enter key is pressed.keyuptokeydown1.A sample of above fixes look like: