Disa
Disa

Reputation: 1

Append and remove last appended, Javascript

Can anyone provide insight on removing a text field element that was just appended? I haven't been able to get other posted solutions to work with the code I'm using. Thanks in advance.

<html>

    <head>
        <script>
            function append(num) {
                var txt = document.getElementById("result").value;
                txt = txt + num + " ";
                document.getElementById("result").value = txt;
            }
        </script>
    </head>

    <body>
        <form>
            <input type="button" value="A" onclick="append(this.value)">
            <input type="button" value="B" onclick="append(this.value)">
            <input type="button" value="C" onclick="append(this.value)">
            <br>
            <br>
            <input type="text" id="result" size="100">
            <br>
            <br>
            <input type="button" value="Clear Last" onclick="?">
            <input type="reset" value="Clear All">
        </form>
    </body>

</html>

Upvotes: 0

Views: 114

Answers (1)

Mr_Green
Mr_Green

Reputation: 41842

Try this:

 function append(num) {
     if (append.prev) {
         //do something 
         console.log(append.prev);
     }
     var txt = document.getElementById("result").value;
     txt = txt + num + " ";
     document.getElementById("result").value = txt;
     append.prev = num;

 }

Working Fiddle (check in console)

Upvotes: 1

Related Questions