splitting paragraphs into spans










1















I have two paragraphs. First I would like to separate each word in first, wrap it with span with id and then move for example 5 first words into second paragraph (with spaces). The problem is that I don't know if append(' ') is a good idea and the second problem is that after injection of the spans to the second paragraph width of it is too large (it should be 100px with text overlaping to next line juz like in first paragraph



here is my attempt



 <body>
<script type="text/javascript">
$(function()
var obj = $('.p1')
var text = obj.html().split(' '), len = text.length, result = ;
for( var i = 0; i < len; i++ )
result[i] = '<span id="'+i+'">' + text[i] + '</span>';

obj.html(result.join(' '));

var words = $('.p1').find('span');
for(var i = 0; i < 5; i++)
$('.p2').append($(words[i]).clone());
$('.p2').append('&nbsp;');

);
</script>

<div class="test" style="width:100px">
<p class="p1">
test1 test2 test3 test4 test5 test6 test7 test8
</p>
</div>
<div class="test" style="width:100px">
<p class="p2">
</p>
</div>
</body>









share|improve this question




























    1















    I have two paragraphs. First I would like to separate each word in first, wrap it with span with id and then move for example 5 first words into second paragraph (with spaces). The problem is that I don't know if append(' ') is a good idea and the second problem is that after injection of the spans to the second paragraph width of it is too large (it should be 100px with text overlaping to next line juz like in first paragraph



    here is my attempt



     <body>
    <script type="text/javascript">
    $(function()
    var obj = $('.p1')
    var text = obj.html().split(' '), len = text.length, result = ;
    for( var i = 0; i < len; i++ )
    result[i] = '<span id="'+i+'">' + text[i] + '</span>';

    obj.html(result.join(' '));

    var words = $('.p1').find('span');
    for(var i = 0; i < 5; i++)
    $('.p2').append($(words[i]).clone());
    $('.p2').append('&nbsp;');

    );
    </script>

    <div class="test" style="width:100px">
    <p class="p1">
    test1 test2 test3 test4 test5 test6 test7 test8
    </p>
    </div>
    <div class="test" style="width:100px">
    <p class="p2">
    </p>
    </div>
    </body>









    share|improve this question


























      1












      1








      1








      I have two paragraphs. First I would like to separate each word in first, wrap it with span with id and then move for example 5 first words into second paragraph (with spaces). The problem is that I don't know if append(' ') is a good idea and the second problem is that after injection of the spans to the second paragraph width of it is too large (it should be 100px with text overlaping to next line juz like in first paragraph



      here is my attempt



       <body>
      <script type="text/javascript">
      $(function()
      var obj = $('.p1')
      var text = obj.html().split(' '), len = text.length, result = ;
      for( var i = 0; i < len; i++ )
      result[i] = '<span id="'+i+'">' + text[i] + '</span>';

      obj.html(result.join(' '));

      var words = $('.p1').find('span');
      for(var i = 0; i < 5; i++)
      $('.p2').append($(words[i]).clone());
      $('.p2').append('&nbsp;');

      );
      </script>

      <div class="test" style="width:100px">
      <p class="p1">
      test1 test2 test3 test4 test5 test6 test7 test8
      </p>
      </div>
      <div class="test" style="width:100px">
      <p class="p2">
      </p>
      </div>
      </body>









      share|improve this question
















      I have two paragraphs. First I would like to separate each word in first, wrap it with span with id and then move for example 5 first words into second paragraph (with spaces). The problem is that I don't know if append(' ') is a good idea and the second problem is that after injection of the spans to the second paragraph width of it is too large (it should be 100px with text overlaping to next line juz like in first paragraph



      here is my attempt



       <body>
      <script type="text/javascript">
      $(function()
      var obj = $('.p1')
      var text = obj.html().split(' '), len = text.length, result = ;
      for( var i = 0; i < len; i++ )
      result[i] = '<span id="'+i+'">' + text[i] + '</span>';

      obj.html(result.join(' '));

      var words = $('.p1').find('span');
      for(var i = 0; i < 5; i++)
      $('.p2').append($(words[i]).clone());
      $('.p2').append('&nbsp;');

      );
      </script>

      <div class="test" style="width:100px">
      <p class="p1">
      test1 test2 test3 test4 test5 test6 test7 test8
      </p>
      </div>
      <div class="test" style="width:100px">
      <p class="p2">
      </p>
      </div>
      </body>






      javascript jquery html






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 4:37









      Cœur

      19.1k9114155




      19.1k9114155










      asked Sep 25 '12 at 23:35









      grubergruber

      9,97128105203




      9,97128105203






















          3 Answers
          3






          active

          oldest

          votes


















          1














          use $('.p2').append(' '); instead of $('.p2').append('&nbsp;');



          &nbsp; = non breaking space






          share|improve this answer























          • :D im so stupid

            – gruber
            Sep 25 '12 at 23:50


















          2














          Something like this?



          $(function() 
          var txt = $('.p1').text().split(' ')
          len = txt.length,
          result = ;
          for (var i = 0; i < len; i++)
          result[i] = '<span id="' + i + '">' + txt[i] + '</span>';

          $('.p1').html(result.join(' '));
          $('.p1 span:gt(0)').appendTo('.p2');
          );​


          http://jsfiddle.net/ULmVt/



          Note that if you want to move the elements, you should not clone them.






          share|improve this answer
































            1














            jsBin demo



             var obj = $('.p1'),
            text = obj.html().split(' '),
            len = text.length,
            result = ;

            for( i=0; i<len; i++ )
            result[i] = '<span id="'+i+'">' + text[i] + '</span>';

            obj.html(result.join(' '));

            $('.p1 span:lt(5)').clone().appendTo('.p2').after(' ');





            share|improve this answer






















              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12592611%2fsplitting-paragraphs-into-spans%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              use $('.p2').append(' '); instead of $('.p2').append('&nbsp;');



              &nbsp; = non breaking space






              share|improve this answer























              • :D im so stupid

                – gruber
                Sep 25 '12 at 23:50















              1














              use $('.p2').append(' '); instead of $('.p2').append('&nbsp;');



              &nbsp; = non breaking space






              share|improve this answer























              • :D im so stupid

                – gruber
                Sep 25 '12 at 23:50













              1












              1








              1







              use $('.p2').append(' '); instead of $('.p2').append('&nbsp;');



              &nbsp; = non breaking space






              share|improve this answer













              use $('.p2').append(' '); instead of $('.p2').append('&nbsp;');



              &nbsp; = non breaking space







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Sep 25 '12 at 23:48









              ReflectiveReflective

              2,7941820




              2,7941820












              • :D im so stupid

                – gruber
                Sep 25 '12 at 23:50

















              • :D im so stupid

                – gruber
                Sep 25 '12 at 23:50
















              :D im so stupid

              – gruber
              Sep 25 '12 at 23:50





              :D im so stupid

              – gruber
              Sep 25 '12 at 23:50













              2














              Something like this?



              $(function() 
              var txt = $('.p1').text().split(' ')
              len = txt.length,
              result = ;
              for (var i = 0; i < len; i++)
              result[i] = '<span id="' + i + '">' + txt[i] + '</span>';

              $('.p1').html(result.join(' '));
              $('.p1 span:gt(0)').appendTo('.p2');
              );​


              http://jsfiddle.net/ULmVt/



              Note that if you want to move the elements, you should not clone them.






              share|improve this answer





























                2














                Something like this?



                $(function() 
                var txt = $('.p1').text().split(' ')
                len = txt.length,
                result = ;
                for (var i = 0; i < len; i++)
                result[i] = '<span id="' + i + '">' + txt[i] + '</span>';

                $('.p1').html(result.join(' '));
                $('.p1 span:gt(0)').appendTo('.p2');
                );​


                http://jsfiddle.net/ULmVt/



                Note that if you want to move the elements, you should not clone them.






                share|improve this answer



























                  2












                  2








                  2







                  Something like this?



                  $(function() 
                  var txt = $('.p1').text().split(' ')
                  len = txt.length,
                  result = ;
                  for (var i = 0; i < len; i++)
                  result[i] = '<span id="' + i + '">' + txt[i] + '</span>';

                  $('.p1').html(result.join(' '));
                  $('.p1 span:gt(0)').appendTo('.p2');
                  );​


                  http://jsfiddle.net/ULmVt/



                  Note that if you want to move the elements, you should not clone them.






                  share|improve this answer















                  Something like this?



                  $(function() 
                  var txt = $('.p1').text().split(' ')
                  len = txt.length,
                  result = ;
                  for (var i = 0; i < len; i++)
                  result[i] = '<span id="' + i + '">' + txt[i] + '</span>';

                  $('.p1').html(result.join(' '));
                  $('.p1 span:gt(0)').appendTo('.p2');
                  );​


                  http://jsfiddle.net/ULmVt/



                  Note that if you want to move the elements, you should not clone them.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Sep 26 '12 at 0:05

























                  answered Sep 25 '12 at 23:46









                  undefinedundefined

                  125k12132171




                  125k12132171





















                      1














                      jsBin demo



                       var obj = $('.p1'),
                      text = obj.html().split(' '),
                      len = text.length,
                      result = ;

                      for( i=0; i<len; i++ )
                      result[i] = '<span id="'+i+'">' + text[i] + '</span>';

                      obj.html(result.join(' '));

                      $('.p1 span:lt(5)').clone().appendTo('.p2').after(' ');





                      share|improve this answer



























                        1














                        jsBin demo



                         var obj = $('.p1'),
                        text = obj.html().split(' '),
                        len = text.length,
                        result = ;

                        for( i=0; i<len; i++ )
                        result[i] = '<span id="'+i+'">' + text[i] + '</span>';

                        obj.html(result.join(' '));

                        $('.p1 span:lt(5)').clone().appendTo('.p2').after(' ');





                        share|improve this answer

























                          1












                          1








                          1







                          jsBin demo



                           var obj = $('.p1'),
                          text = obj.html().split(' '),
                          len = text.length,
                          result = ;

                          for( i=0; i<len; i++ )
                          result[i] = '<span id="'+i+'">' + text[i] + '</span>';

                          obj.html(result.join(' '));

                          $('.p1 span:lt(5)').clone().appendTo('.p2').after(' ');





                          share|improve this answer













                          jsBin demo



                           var obj = $('.p1'),
                          text = obj.html().split(' '),
                          len = text.length,
                          result = ;

                          for( i=0; i<len; i++ )
                          result[i] = '<span id="'+i+'">' + text[i] + '</span>';

                          obj.html(result.join(' '));

                          $('.p1 span:lt(5)').clone().appendTo('.p2').after(' ');






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Sep 25 '12 at 23:50









                          Roko C. BuljanRoko C. Buljan

                          128k21200230




                          128k21200230



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12592611%2fsplitting-paragraphs-into-spans%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Top Tejano songwriter Luis Silva dead of heart attack at 64

                              政党

                              天津地下鉄3号線