Ad

The Value Gets Copied To The Textarea But Disappears After A Moment

- 1 answer

I am trying to copy the value of the textbox to the textarea However the value gets copied using the javascript function but it disappears from the textarea after a second. What am i doing wrong?Why does it get disappear after being copied? this is the html:

<html>
<head>
    <title>
    </title>
    <script src="scripts/script.js" type="text/javascript"></script>
</head>
<body>
    <form>
           <label>Key/Value Pair: </label><input type="text" name="inputText" id="t1"></br></br>
           <label>Key/Value List: </label><br>
           <textarea name="outputText" rows="10" cols="50"  id="t2" ></textarea><br><br>
           <input type="submit" value="Add" onClick="fn_copy()" />
    </form>


</body>

and this is the javascript code:

function fn_copy()
{
    var temp = document.getElementById("t1").value;
    if(temp != "")
    {
        document.getElementById("t2").value = temp;
    }
    else
        alert("Text is Empty");
}

Thank you.

Ad

Answer

Change your button type to button instead of submit. Otherwise your page will be refreshed (default behavior with submit) and hence the content of your textarea reset.

<input type="button" value="Add" onClick="fn_copy()" />

Ad
source: stackoverflow.com
Ad