How would you append a
tag on a value inside a textarea, it only appends it after a keypress of SHIFT+ENTER,
here is my code to append on the text area and it does not work??
$('#textarea').append("<br/>");
i think there are still something lacking.
DEMO
Thank you. . .
How would you append a
tag on a value inside a textarea, it only appends it after a keypress of SHIFT+ENTER,
here is my code to append on the text area and it does not work??
$('#textarea').append("<br/>");
i think there are still something lacking.
DEMO
Thank you. . .
Share Improve this question asked Jun 7, 2012 at 6:11 tomexsanstomexsans 4,5274 gold badges37 silver badges49 bronze badges 1-
\n
is newline;<br>
is not. – Derek 朕會功夫 Commented Jun 7, 2012 at 6:20
2 Answers
Reset to default 8Are you actually wanting to place the characters in the value?
$("textarea").val(function(i,v){
return v + "<br/>";
});
Or simply add a new line?
$("textarea").val(function(i,v){
return v + "\nfoo";
});
Fiddle: http://jsfiddle/jonathansampson/SNeyy/
If you want to respond only to shift + enter:
$("textarea").on("keypress", function(e){
if ( e.which === 13 && e.shiftKey ) {
$(this).val(function(i,v){
return v + "<br/>"; // or return v + "\n"; (whatever you want)
});
}
});
Fiddle: http://jsfiddle/jonathansampson/SNeyy/1/
You are trying to change the value of the textarea
. To add the string "<br/>" you have to use
$('#textarea').val($('#textarea').val() + '<br/>');
and to add a new line you have to use
$('#textarea').val($('#textarea').val() + "\n");
Textarea can have only a text node as its child. <textarea><br/></textarea>
is incorrect usage. If you want to add the string "<br/>", the html code should be
<textarea><br/></textarea>
and to add a new line, the html code should be
<textarea>
</textarea>
In other words, you cannot use <textarea><br/></textarea>