Home > Enterprise >  [object HTMLInputElement]
[object HTMLInputElement]

Time:12-08

I made a chat app, but this is just the server. I'm not very good at JS, but this code doesn't do what it's supposed to.

from flask import Flask
app = Flask('app')

@app.route('/')
def hello_world():
  return 'Chat app \
          <br> \
          <h1 id="p1">text<h1> \
          <input id="text2"> \
          <button onclick="chat()">Chat</button> \
          <script> \
          function chat(){ \
            document.getElementById("p1").innerHTML = document.getElementById("text2"); \
          } \
          </script>'

app.run(host='0.0.0.0', port=8080)

It should replace p1 with the text of text2, but instead it replaces it with "[object HTMLInputElement]."

Can you help me?

CodePudding user response:

document.getElementById("p1").innerHTML = document.getElementById("text2")

will return a string version of element itself. It seems like you want the value of the element, which will be the user input:

function chat(){
    document.getElementById("p1").innerHTML =   document.getElementById("text2").value;
}
Chat app
<br>
<h1 id="p1">text<h1>
<input id="text2">
<button onclick="chat()">Chat</button>

CodePudding user response:

 document.getElementById("p1").innerHTML = document.getElementById("text2").value
  • Related