skip to Main Content

how do you take an input from the user and use it as pop-up text in html using javascript?

i tried using html input tags but it didn’t give me what i expected. I don’t know how to take the user input from the input tag and use the same input in javascript

2

Answers


  1. If you want to update something as the user is typing, you can add a listener for the input event on the <input> element, like below.

    document.getElementById("myinput").addEventListener("input", (e) => {
      const inputText = e.target.value
      console.log(inputText)
    })
    <input id="myinput" type="text">

    If you just want to get the value in the middle of some other function, you can access the .value property of the <input> element, like below.

    function myfunction() {
      const inputText = document.getElementById("myinput").value
      console.log(inputText)
    }
    <input id="myinput" type="text">
    <button onclick="myfunction();">Click me!</button>
    Login or Signup to reply.
  2. function myfunction() {
      const inputText = document.getElementById("myinput").value
      console.log(inputText)
      alert(inputText
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search