splitting paragraphs into spans
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(' ');
);
</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
add a comment |
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(' ');
);
</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
add a comment |
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(' ');
);
</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
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(' ');
);
</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
javascript jquery html
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
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
use $('.p2').append(' ');
instead of $('.p2').append(' ');
= non breaking space
:D im so stupid
– gruber
Sep 25 '12 at 23:50
add a comment |
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.
add a comment |
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(' ');
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
use $('.p2').append(' ');
instead of $('.p2').append(' ');
= non breaking space
:D im so stupid
– gruber
Sep 25 '12 at 23:50
add a comment |
use $('.p2').append(' ');
instead of $('.p2').append(' ');
= non breaking space
:D im so stupid
– gruber
Sep 25 '12 at 23:50
add a comment |
use $('.p2').append(' ');
instead of $('.p2').append(' ');
= non breaking space
use $('.p2').append(' ');
instead of $('.p2').append(' ');
= non breaking space
answered Sep 25 '12 at 23:48
ReflectiveReflective
2,7941820
2,7941820
:D im so stupid
– gruber
Sep 25 '12 at 23:50
add a comment |
: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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Sep 26 '12 at 0:05
answered Sep 25 '12 at 23:46
undefinedundefined
125k12132171
125k12132171
add a comment |
add a comment |
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(' ');
add a comment |
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(' ');
add a comment |
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(' ');
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(' ');
answered Sep 25 '12 at 23:50
Roko C. BuljanRoko C. Buljan
128k21200230
128k21200230
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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