sojutyp sojutyp - 4 months ago 15
Javascript Question

Javascript save , erase and reload input value

I have a problem with my Script. I want to do the following steps in this order:

1. Save the text in the input field.

2. Delete all text in the input field.

3. Reload the same text that was deleted before in the input field.

The problem with my script is that the

ug()
- function writes
undefined
in my textbox instead of the string that should be stored in
var exput
. The
alert(exput)
however shows me the correct content.
Help would be very much appreciated. And I'm sure there is better ways to do that, I'm quite new to this stuff.

HTML

<textarea id="a" style="width: 320px; height: 200px;"></textarea>
<input type="checkbox" id="remember" onclick="merker();deleter();ug()" />


Javascript

function merker() {
var merkzeug = document.getElementById('a').value;
ug(merkzeug);
};

function deleter() {
if(document.getElementById('remember').checked == true)
{
document.getElementById('a').value = "";
}
else {document.getElementById('a').value = "";
}
};

function ug(exput) {
alert(exput);
document.getElementById('a').value = exput;
};

Answer

Your code is calling merker(); deleter(); ug(); in the onclick event, but ug() is already called by merker(). You should be doing this instead:

function merker() {
    var merkzeug = document.getElementById('a').value;
    deleter();
    ug(merkzeug);
};

function deleter() {
      if(document.getElementById('remember').checked == true)
      {
        document.getElementById('a').value = "";
      }
      else {document.getElementById('a').value = "";
      }
    };

function ug(exput) {
    alert(exput);
    document.getElementById('a').value = exput;
};
<textarea  id="a" style="width: 320px; height: 200px;"></textarea>
<input type="checkbox" id="remember" onclick="merker();" />