Hello. I have this html:

<div id="a"> <span>Я</span> <span>люблю</span> <span>печеньки</span> </div> <div id="b"></div> 

When I click a button, the following script is executed:

 $('div#b').html($('div#a').children('span').text()) 

I had this problem.
It eventually transfers the text like this:

 <div id="b">Я люблю печеньки</div> 

And how can I make it endure like this:

 <div id="b">Я люблю печеньки</div> 

I hope the problem is understood.)

    1 answer 1

    If, apart from span-elements, there is nothing more in the first block, then you can do it simply:

     $('div#b').html('<pre>' + $('div#a').text() + '</pre>'); 

    If there are other elements, then:

     var divA = $('div#a').clone(); $(':not(span)',divA).remove(); $('div#b').html('<pre>' + divA.text() + '</pre>'); 

    If the <pre> tag does not suit you, then we do the transfers using <br>

     var fStr = $('div#a span').map(function(i, el){ return $(el).text() + '<br>'; }).get().join(''); $('div#b').html(fStr); 

    The last option in the sandbox .

    • Well, in general, I understand. But I need it to transfer not visually, but transfer inside the code. - Yuri
    • Oh, everything. Corrected more code for myself, earned.) - Yuri