user608082
user608082

Reputation: 61

How to get textarea content with jquery

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 and the div's id should be changed dynamically, if i repeat the process.

Please help me to find the solution for the above.

Upvotes: 6

Views: 5977

Answers (2)

Amir
Amir

Reputation: 4111

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');

Upvotes: 0

Aron Rotteveel
Aron Rotteveel

Reputation: 83213

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');

Upvotes: 7

Related Questions