Home > Software engineering >  How to Manipulate the Text element in Html by A Variable whose value is given by the user?
How to Manipulate the Text element in Html by A Variable whose value is given by the user?

Time:12-01

I am trying to Manipulate the Text with id 'varname' (Spartans) with the value inputted in the form saved as a variable 'input'

<div >
    <h2  id="demo">Welcome Back,&nbsp; <span id="varname">Spartan</span></h2>
     <div >
    <form>
        <label for="Name">Enter Your Name</label>
        <input type="text" id="Name">
        <input type="submit" value="Submit" onclick="myFunction()">
    </form>
    </div>
    <script>
        function myFunction(){
            var input= document.getElementById("Name").value;
            document.getElementById("varname").innerHTML = input;
        }
    </script>

CodePudding user response:

Use <input type="button"> instead of <input type="submit">

https://jsfiddle.net/yt0kgnqe/

  • Related