简体   繁体   中英

Getting the value of textarea inside form

I want to get the value of this textarea inside form and copy the the value to another textarea outside form using javascript. How should I do that? here's my textarea...

<form>
    <textarea id="tpName" name="tpName" onchange="tpName(this)" style="margin-top: -9px; width: 275px; height: 40px;"></textarea>
</form>

<textarea id="copytpName" name="copytpName" style="margin-top: -9px; width: 275px; height: 40px;"></textarea>

Whenever I type a value in textarea inside form, I also want to update the value of textarea outside form.

You could do like this in javascript,

HTML

<form>
<textarea id="tpName" name="tpName" onkeyup="copyThis(this);" style="margin-top: -9px; width: 275px; height: 40px;"></textarea>
</form>

<textarea id="copytpName" name="copytpName" style="margin-top: -9px; width: 275px; height: 40px;"></textarea>

JAVASCRIPT

function copyThis(txObj) {
    document.getElementById("copytpName").value = txObj.value;   
}

Note: You need to change the function name. The function name matches with the textarea name, so its creating the issue in your case.

Try this http://jsfiddle.net/CZCnx/2/

There's no need for jQuery as some others have posted in their answer. Simply don't name your function the same thing you use for IDs and form names and it works:

jsFiddle example

<form>
    <textarea id="ttpName" name="ttpName" onchange="tpName(this)" style="margin-top: -9px; width: 275px; height: 40px;"></textarea>
</form>
<textarea id="copytpName" name="copytpName" style="margin-top: -9px; width: 275px; height: 40px;"></textarea>
function tpName(data) {
    document.getElementById("copytpName").value = data.value;
}

I changed your textarea to <textarea id="ttpName" name="ttpName" ...

I would try this:

var text = document.getElementById("tpName").value;

$("#copytpName").attr("value", text);

I have added the code here, it uses jquery

http://jsfiddle.net/5B6KC/1/

CODE:

$("#tpName").keyup(function(){
    $("#copytpName").val($(this).val());
})

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