Javascript – content from divs into textarea

copyhtmljavascriptPHPtextarea

I'd like to transfer text from all div's with specific class to the textarea on the same page.

How can I do that?

for example:

< div class="test1" > Example1 < /div >
< div class="test2" > Example2 < /div >
< div class="test1" > Example3 < /div >
< div class="test3" > Example4 < /div >

I would like to transfer the content of div class test1
and in the textarea should show "Example1" and "Example3".

Any help, please!
javascript or php

john

Best Answer

This would be done pretty easily with jQuery:

var newTextVal = "";
$('.text1').each(
    function()
    {
       newTextVal += $(this).text();
    }
);
 $('textarea').val( newTextVal );

This above will loop through each element with class "text1" and append it's text node value to the text within the textarea.