简体   繁体   中英

Why is this simple Javascript refactor not working?

A simple question about a form submit in HTML.

Why does this work:

var inputStuff = document.getElementById("inputBox");
var output = document.getElementById("outputBox");

function useMethod(element) {

    output.innerText = inputStuff.value;
    return false; 
}

But this doesn't:

var inputStuff = document.getElementById("inputBox");
var output = document.getElementById("outputBox");

function useMethod(element) {
    var out = output.innerText;
    var into = inputStuff.value;

    out = into;
    return false; 
}

Here's the HTML:

<h1>Put your input in here</h1>
    <form onsubmit="return useMethod(this)" action="">
        <input type="text" id="inputBox">
        <input type="submit" value="Submit">
    </form>

    <h2>Output:</h2>
    <p id="outputBox">Starter text</p>

Many thanks in advance for any help,

R

out = into; will simply assign the value of into (string) to out (string), whereas output.innerText = inputStuff.value; will invoke an implicit setter that will change the DOM value as well.

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