简体   繁体   中英

[object HTMLInputElement]

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?

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM