Skip to content
Advertisement

Why can I not overwrite the innerHTML after I once clicked into it

My Code:

JavaScript

As you probably read, the code is for a Real-Time Texteditor Project I’m working on with a friend. But I don’t understand why everything works well until I click on the text field and wanna write something. The server gets everything and the other instance also. The only problem is, that the second instance doesn’t overwrite the innerHTML of the text field. If I reload the second instance everything appears in the text field. I’m confused. Does anyone know why this is happening?

Advertisement

Answer

First of all, you shouldn’t set an Interval to load data every second, as in a normal text editor, you load data every time the textarea’s value is changed. When setting an Interval to load data every second you’re essentially spamming the user and the server for no reason.

JavaScript

All I changed was combining the load_data function with the onTextUpdate function, and changing document.getElementById("textarea").innerHTML = response.data to document.getElementById("textarea").value = response.data, since as Teemu explained well in the comments: textarea can’t contain HTML, it contains plain text only. Use value instead of innerHTML.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement