Grabbing value and appending to textarea with Javascript? - Stack Overflow

I'm stuck!I have this simple form: <p><input type="text" name="hometown&qu

I'm stuck!

I have this simple form:

<p><input type="text" name="hometown" id="hometown" size="22" /></p>
<p><textarea name="ment" id="ment"></textarea></p>

What I need is to append the input value from #hometown to textarea! It mustn't replace text already written there. In the best case, it'd just print at the end of whatever is written on ''submit'' click.

This is how far I've got with my Javascript, but nothing seems to work.

function addtxt(input) {
    var hometown = document.getElementById('hometown').value;
    var obj=document.getElementById(ment)
    var txt=document.createTextNode(lol)
    obj.appendChild(txt)
}

I'm stuck!

I have this simple form:

<p><input type="text" name="hometown" id="hometown" size="22" /></p>
<p><textarea name="ment" id="ment"></textarea></p>

What I need is to append the input value from #hometown to textarea! It mustn't replace text already written there. In the best case, it'd just print at the end of whatever is written on ''submit'' click.

This is how far I've got with my Javascript, but nothing seems to work.

function addtxt(input) {
    var hometown = document.getElementById('hometown').value;
    var obj=document.getElementById(ment)
    var txt=document.createTextNode(lol)
    obj.appendChild(txt)
}
Share Improve this question edited Apr 26, 2013 at 11:20 Jake1164 12.4k6 gold badges48 silver badges65 bronze badges asked Apr 26, 2013 at 11:14 user2307598user2307598 331 silver badge5 bronze badges 1
  • Please post the Javascript code, u have written? – Shubh Commented Apr 26, 2013 at 11:16
Add a ment  | 

3 Answers 3

Reset to default 3

Textarea has value property to operate with its contents. Just use += to append text:

document.getElementById("ment").value +=
    document.getElementById("hometown").value;

Try this

var oldval=$('#ment').val();
var newval=$('#hometown').val();
S('#ment').val(oldval+' '+newval);

Here's an example for you I've put on JSFiddle, using pure javascript and the onClick listener

http://jsfiddle/vyqWx/1/

HTML

<input type="text" name="hometown" id="hometown" size="22" />
<textarea name="ment" id="ment"></textarea>
<input type="submit" onClick="doMagic();">

JS

function doMagic(){    
    var homeTown = document.getElementById("hometown").value;    
    document.getElementById("ment").value += homeTown;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745084516a4610325.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信