简体   繁体   中英

Get input from textbox and write it below

I need to take the value from an input box and write it below the input box on the click of a button. I thought to use a label but if there is another way I am open to suggestions.

My code so far:

    <h1>Test</h1>
    <form name="greeting">
        Type your name here: <input type = "Text" name="fullname" id="name"> <button onclick="getName()">Create</button><br>
        Hello <label id="greet">Hello</label> 
    </form>
    <script lang="javascript">
     function getName() {
      var inputVal = document.getElementById("name").value;
      if (inputVal == "") {
                             document.getElementById("name").style.backgroundColor = "red";
                          }
     else {
            document.write("Hello " + document.getElementById("name"));
          }

First of all, you don't want to submit a form, so change button type from "submit" (default) to "button".

Then you should not use document.write almost never, it's used in very specific cases. Use proper DOM manipulation methods like appendChild . I would use convenient insertAdjacentHTML :

 function getName() { var input = document.getElementById("name"); if (input.value == "") { input.style.backgroundColor = "red"; } else { input.insertAdjacentHTML('afterend', '<div>' + input.value + '</div>'); } } 
 <form name="greeting">Type your name here: <input type="Text" name="fullname" id="name" /> <button type="button" onclick="getName()">Create</button> <br>Hello <label id="greet">Hello</label> </form> 

First you need to stop your form from submitting. Second you should not use document.write, since it will not append the text as wanted after the input field. And last you need to validate the elements value and not the element itself.

<html>
    <head>
        <script>
            //First put the function in the head.
            function getName(){
                var input = document.getElementById("name");
                input.style.backgroundColor = ''; //Reseting the backgroundcolor

                if (input.value == ''){ //Add the.value
                    input.style.backgroundColor = 'red';
                }
                else{
                    //document.write('Hello ' + input.value); //This would overwrite the whole document, removing your dom.

                    //Instead we write it in your greeting field.
                    var tE = document.getElementById('greet');
                    tE.innerHTML = input.value;
                }

                return false //Prevent the form from being submitted.
            }
        </script>
    </head>

    <body>
        <h1>Test</h1>
        <form name = 'greeting'>
            Type your name here: <input type = "Text" name="fullname" id="name"> <button onclick="return getName()">Create</button><br>
            Hello <label id="greet">Hello</label> 
        </form>
    </body>
</html>

You need to cancel the submit event which makes the form submit, alternatively you could not wrap everything inside a form element and just use normal div that way submit button wont submit.

Demo : https://jsfiddle.net/bypr0z5a/

Note reason i attach event handler in javascript and note onclick attribute on button element is because jsfiddle works weird, on ordinary page your way of calling getName() would have worked.

byId('subBtn').onclick = function (e) {
    e.preventDefault();
    var i = byId('name'),
        inputVal = i.value;

    if (inputVal == "") {
        i.style.backgroundColor = "red";
    } else {
        byId('greet').innerText = inputVal;
        i.style.backgroundColor = "#fff";
    }
}

function byId(x) {
    return document.getElementById(x);
}

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