I am new to jquery. I got struck with some function, I have two textarea boxes, let us suppose
the first textarea id is first_ta the second textarea id is second_ta
<textarea id="first_ta" rows="2" cols="2"></textarea>
<textarea id="second_ta" rows="2" cols="2"></textarea>
1. I want the content of first_ta in a "p" tag and the tag should be generated by jquery itself.
2. I want the content of second_ta in a "div" tag that should be generated by jquery开发者_如何学Go and the div's id should be changed dynamically, if i repeat the process.
Please help me to find the solution for the above.
I want the content of first_ta in a "p" tag and the tag should be generated by jquery itself.
$('<p>').html($('#first_ta').val()).appendTo('body');
I want the content of second_ta in a "div" tag that should be generated by jquery and the div's id
// assuming you've got a variable genId defined somewhere in your code with a start
// value of 1
$('<div>').html($('#second_ta').val()).attr('id', 'generated-id-' + genId++).appendTo('body');
The same as @Aren reply but just add encodeURIComponent for reading textarea value
$('<p>').html(encodeURIComponent($('#first_ta').val())).appendTo('body');
$('<div>').html(encodeURIComponent($('#second_ta').val())).attr('id', 'generated-id-' + genId++).appendTo('body');
精彩评论