Find and wrap in a span using JS/jQuery
As the title asks I'm searching for a way to target all occurrences of <br><br>
in a document so that I can wrap them in a span.
From what I can gather they don't exist as a string so I'm unsure how to target them.
The end goal is to take something like this:
Hello my name is Neil.<br><br>How are you?
And make it into something like this:
Hello my name is Neil.<span style="line-height:14px;"><br><br></span>How are you?
javascript jquery html
add a comment |
As the title asks I'm searching for a way to target all occurrences of <br><br>
in a document so that I can wrap them in a span.
From what I can gather they don't exist as a string so I'm unsure how to target them.
The end goal is to take something like this:
Hello my name is Neil.<br><br>How are you?
And make it into something like this:
Hello my name is Neil.<span style="line-height:14px;"><br><br></span>How are you?
javascript jquery html
This may help: stackoverflow.com/questions/3337587/…
– Blake F.
Nov 13 '18 at 15:41
3
Couldn't you use css rule instead?brline-height:14px;
?
– charlietfl
Nov 13 '18 at 15:41
Why wrap them in a span ? maybe there is another solution for your problem
– Mihai T
Nov 13 '18 at 15:48
You don't need to wrapbr
elements in aspan
just to setline-height
on them, do it directly in CSS
– Rory McCrossan
Nov 13 '18 at 15:51
@charlietfl @rorymccrossan the OP is probably confronted to an existing content where<br><br>
is legion and he wants to treat only this case without changing anything to the existing single<br>
tags.
– Laurent S.
Nov 13 '18 at 16:06
add a comment |
As the title asks I'm searching for a way to target all occurrences of <br><br>
in a document so that I can wrap them in a span.
From what I can gather they don't exist as a string so I'm unsure how to target them.
The end goal is to take something like this:
Hello my name is Neil.<br><br>How are you?
And make it into something like this:
Hello my name is Neil.<span style="line-height:14px;"><br><br></span>How are you?
javascript jquery html
As the title asks I'm searching for a way to target all occurrences of <br><br>
in a document so that I can wrap them in a span.
From what I can gather they don't exist as a string so I'm unsure how to target them.
The end goal is to take something like this:
Hello my name is Neil.<br><br>How are you?
And make it into something like this:
Hello my name is Neil.<span style="line-height:14px;"><br><br></span>How are you?
javascript jquery html
javascript jquery html
asked Nov 13 '18 at 15:36
Neil MorganNeil Morgan
196
196
This may help: stackoverflow.com/questions/3337587/…
– Blake F.
Nov 13 '18 at 15:41
3
Couldn't you use css rule instead?brline-height:14px;
?
– charlietfl
Nov 13 '18 at 15:41
Why wrap them in a span ? maybe there is another solution for your problem
– Mihai T
Nov 13 '18 at 15:48
You don't need to wrapbr
elements in aspan
just to setline-height
on them, do it directly in CSS
– Rory McCrossan
Nov 13 '18 at 15:51
@charlietfl @rorymccrossan the OP is probably confronted to an existing content where<br><br>
is legion and he wants to treat only this case without changing anything to the existing single<br>
tags.
– Laurent S.
Nov 13 '18 at 16:06
add a comment |
This may help: stackoverflow.com/questions/3337587/…
– Blake F.
Nov 13 '18 at 15:41
3
Couldn't you use css rule instead?brline-height:14px;
?
– charlietfl
Nov 13 '18 at 15:41
Why wrap them in a span ? maybe there is another solution for your problem
– Mihai T
Nov 13 '18 at 15:48
You don't need to wrapbr
elements in aspan
just to setline-height
on them, do it directly in CSS
– Rory McCrossan
Nov 13 '18 at 15:51
@charlietfl @rorymccrossan the OP is probably confronted to an existing content where<br><br>
is legion and he wants to treat only this case without changing anything to the existing single<br>
tags.
– Laurent S.
Nov 13 '18 at 16:06
This may help: stackoverflow.com/questions/3337587/…
– Blake F.
Nov 13 '18 at 15:41
This may help: stackoverflow.com/questions/3337587/…
– Blake F.
Nov 13 '18 at 15:41
3
3
Couldn't you use css rule instead?
brline-height:14px;
?– charlietfl
Nov 13 '18 at 15:41
Couldn't you use css rule instead?
brline-height:14px;
?– charlietfl
Nov 13 '18 at 15:41
Why wrap them in a span ? maybe there is another solution for your problem
– Mihai T
Nov 13 '18 at 15:48
Why wrap them in a span ? maybe there is another solution for your problem
– Mihai T
Nov 13 '18 at 15:48
You don't need to wrap
br
elements in a span
just to set line-height
on them, do it directly in CSS– Rory McCrossan
Nov 13 '18 at 15:51
You don't need to wrap
br
elements in a span
just to set line-height
on them, do it directly in CSS– Rory McCrossan
Nov 13 '18 at 15:51
@charlietfl @rorymccrossan the OP is probably confronted to an existing content where
<br><br>
is legion and he wants to treat only this case without changing anything to the existing single <br>
tags.– Laurent S.
Nov 13 '18 at 16:06
@charlietfl @rorymccrossan the OP is probably confronted to an existing content where
<br><br>
is legion and he wants to treat only this case without changing anything to the existing single <br>
tags.– Laurent S.
Nov 13 '18 at 16:06
add a comment |
2 Answers
2
active
oldest
votes
I won't discuss the premise although I'm quite sure there are more elegant ways of doing what you actually want to do. This should do the trick:
document.body.outerHTML = document.body.outerHTML.replace(/<br><br>/gi, '<span style="line-height:14px;"><br><br></span>')
<div>
This is a test <br><br> and it's working
</div>
Addition following the comment of charlietfl: Note that one of the caveat of this is that it would just drop all event listeners on the content being replaced like this, because you're actually dropping all elements of the dom and rebuilding a new one. A workaround to this issue would be to do this replacement before any other binding. Another workaround would be limit the scope of this replacement: right now it occurs on the whole body, but you could be more accurately targeting paragraphs, or paragraphs with a certain class.
I also used this edit to make the regex match case insensitive so that <BR><BR>
is treated as <br><br>
or whatever camel case you would be using...
2
Caveat would be it will remove all existing event listeners
– charlietfl
Nov 13 '18 at 15:54
This also re-invokes the document parser, layout engine, and repaints the entire page. On a mid-tier device with a moderately complex page that could cost you several seconds for a small visual change that can be addressed in another manner.
– coreyward
Nov 13 '18 at 17:14
add a comment |
You can do this, but it'll wrap single <br>
elements as well:
$("br").wrapAll('<span style="line-height:14px" />');
If you're only setting line-height or other similar styles you may just want to use your styles instead:
br
line-height: 14px;
/* Hide second line-break */
br + br
display: none;
I like the CSS approach of this answer. Main issue is the functional goal of OP is unclear, but depending on the needs this approach might be the best and most elegant one (besides rewriting content to be semantic...).
– Laurent S.
Nov 13 '18 at 16:08
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%2f53284454%2ffind-and-wrap-brbr-in-a-span-using-js-jquery%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I won't discuss the premise although I'm quite sure there are more elegant ways of doing what you actually want to do. This should do the trick:
document.body.outerHTML = document.body.outerHTML.replace(/<br><br>/gi, '<span style="line-height:14px;"><br><br></span>')
<div>
This is a test <br><br> and it's working
</div>
Addition following the comment of charlietfl: Note that one of the caveat of this is that it would just drop all event listeners on the content being replaced like this, because you're actually dropping all elements of the dom and rebuilding a new one. A workaround to this issue would be to do this replacement before any other binding. Another workaround would be limit the scope of this replacement: right now it occurs on the whole body, but you could be more accurately targeting paragraphs, or paragraphs with a certain class.
I also used this edit to make the regex match case insensitive so that <BR><BR>
is treated as <br><br>
or whatever camel case you would be using...
2
Caveat would be it will remove all existing event listeners
– charlietfl
Nov 13 '18 at 15:54
This also re-invokes the document parser, layout engine, and repaints the entire page. On a mid-tier device with a moderately complex page that could cost you several seconds for a small visual change that can be addressed in another manner.
– coreyward
Nov 13 '18 at 17:14
add a comment |
I won't discuss the premise although I'm quite sure there are more elegant ways of doing what you actually want to do. This should do the trick:
document.body.outerHTML = document.body.outerHTML.replace(/<br><br>/gi, '<span style="line-height:14px;"><br><br></span>')
<div>
This is a test <br><br> and it's working
</div>
Addition following the comment of charlietfl: Note that one of the caveat of this is that it would just drop all event listeners on the content being replaced like this, because you're actually dropping all elements of the dom and rebuilding a new one. A workaround to this issue would be to do this replacement before any other binding. Another workaround would be limit the scope of this replacement: right now it occurs on the whole body, but you could be more accurately targeting paragraphs, or paragraphs with a certain class.
I also used this edit to make the regex match case insensitive so that <BR><BR>
is treated as <br><br>
or whatever camel case you would be using...
2
Caveat would be it will remove all existing event listeners
– charlietfl
Nov 13 '18 at 15:54
This also re-invokes the document parser, layout engine, and repaints the entire page. On a mid-tier device with a moderately complex page that could cost you several seconds for a small visual change that can be addressed in another manner.
– coreyward
Nov 13 '18 at 17:14
add a comment |
I won't discuss the premise although I'm quite sure there are more elegant ways of doing what you actually want to do. This should do the trick:
document.body.outerHTML = document.body.outerHTML.replace(/<br><br>/gi, '<span style="line-height:14px;"><br><br></span>')
<div>
This is a test <br><br> and it's working
</div>
Addition following the comment of charlietfl: Note that one of the caveat of this is that it would just drop all event listeners on the content being replaced like this, because you're actually dropping all elements of the dom and rebuilding a new one. A workaround to this issue would be to do this replacement before any other binding. Another workaround would be limit the scope of this replacement: right now it occurs on the whole body, but you could be more accurately targeting paragraphs, or paragraphs with a certain class.
I also used this edit to make the regex match case insensitive so that <BR><BR>
is treated as <br><br>
or whatever camel case you would be using...
I won't discuss the premise although I'm quite sure there are more elegant ways of doing what you actually want to do. This should do the trick:
document.body.outerHTML = document.body.outerHTML.replace(/<br><br>/gi, '<span style="line-height:14px;"><br><br></span>')
<div>
This is a test <br><br> and it's working
</div>
Addition following the comment of charlietfl: Note that one of the caveat of this is that it would just drop all event listeners on the content being replaced like this, because you're actually dropping all elements of the dom and rebuilding a new one. A workaround to this issue would be to do this replacement before any other binding. Another workaround would be limit the scope of this replacement: right now it occurs on the whole body, but you could be more accurately targeting paragraphs, or paragraphs with a certain class.
I also used this edit to make the regex match case insensitive so that <BR><BR>
is treated as <br><br>
or whatever camel case you would be using...
document.body.outerHTML = document.body.outerHTML.replace(/<br><br>/gi, '<span style="line-height:14px;"><br><br></span>')
<div>
This is a test <br><br> and it's working
</div>
document.body.outerHTML = document.body.outerHTML.replace(/<br><br>/gi, '<span style="line-height:14px;"><br><br></span>')
<div>
This is a test <br><br> and it's working
</div>
edited Nov 13 '18 at 16:02
answered Nov 13 '18 at 15:53
Laurent S.Laurent S.
5,55422135
5,55422135
2
Caveat would be it will remove all existing event listeners
– charlietfl
Nov 13 '18 at 15:54
This also re-invokes the document parser, layout engine, and repaints the entire page. On a mid-tier device with a moderately complex page that could cost you several seconds for a small visual change that can be addressed in another manner.
– coreyward
Nov 13 '18 at 17:14
add a comment |
2
Caveat would be it will remove all existing event listeners
– charlietfl
Nov 13 '18 at 15:54
This also re-invokes the document parser, layout engine, and repaints the entire page. On a mid-tier device with a moderately complex page that could cost you several seconds for a small visual change that can be addressed in another manner.
– coreyward
Nov 13 '18 at 17:14
2
2
Caveat would be it will remove all existing event listeners
– charlietfl
Nov 13 '18 at 15:54
Caveat would be it will remove all existing event listeners
– charlietfl
Nov 13 '18 at 15:54
This also re-invokes the document parser, layout engine, and repaints the entire page. On a mid-tier device with a moderately complex page that could cost you several seconds for a small visual change that can be addressed in another manner.
– coreyward
Nov 13 '18 at 17:14
This also re-invokes the document parser, layout engine, and repaints the entire page. On a mid-tier device with a moderately complex page that could cost you several seconds for a small visual change that can be addressed in another manner.
– coreyward
Nov 13 '18 at 17:14
add a comment |
You can do this, but it'll wrap single <br>
elements as well:
$("br").wrapAll('<span style="line-height:14px" />');
If you're only setting line-height or other similar styles you may just want to use your styles instead:
br
line-height: 14px;
/* Hide second line-break */
br + br
display: none;
I like the CSS approach of this answer. Main issue is the functional goal of OP is unclear, but depending on the needs this approach might be the best and most elegant one (besides rewriting content to be semantic...).
– Laurent S.
Nov 13 '18 at 16:08
add a comment |
You can do this, but it'll wrap single <br>
elements as well:
$("br").wrapAll('<span style="line-height:14px" />');
If you're only setting line-height or other similar styles you may just want to use your styles instead:
br
line-height: 14px;
/* Hide second line-break */
br + br
display: none;
I like the CSS approach of this answer. Main issue is the functional goal of OP is unclear, but depending on the needs this approach might be the best and most elegant one (besides rewriting content to be semantic...).
– Laurent S.
Nov 13 '18 at 16:08
add a comment |
You can do this, but it'll wrap single <br>
elements as well:
$("br").wrapAll('<span style="line-height:14px" />');
If you're only setting line-height or other similar styles you may just want to use your styles instead:
br
line-height: 14px;
/* Hide second line-break */
br + br
display: none;
You can do this, but it'll wrap single <br>
elements as well:
$("br").wrapAll('<span style="line-height:14px" />');
If you're only setting line-height or other similar styles you may just want to use your styles instead:
br
line-height: 14px;
/* Hide second line-break */
br + br
display: none;
edited Nov 13 '18 at 17:16
answered Nov 13 '18 at 15:45
coreywardcoreyward
50.9k1598126
50.9k1598126
I like the CSS approach of this answer. Main issue is the functional goal of OP is unclear, but depending on the needs this approach might be the best and most elegant one (besides rewriting content to be semantic...).
– Laurent S.
Nov 13 '18 at 16:08
add a comment |
I like the CSS approach of this answer. Main issue is the functional goal of OP is unclear, but depending on the needs this approach might be the best and most elegant one (besides rewriting content to be semantic...).
– Laurent S.
Nov 13 '18 at 16:08
I like the CSS approach of this answer. Main issue is the functional goal of OP is unclear, but depending on the needs this approach might be the best and most elegant one (besides rewriting content to be semantic...).
– Laurent S.
Nov 13 '18 at 16:08
I like the CSS approach of this answer. Main issue is the functional goal of OP is unclear, but depending on the needs this approach might be the best and most elegant one (besides rewriting content to be semantic...).
– Laurent S.
Nov 13 '18 at 16:08
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%2f53284454%2ffind-and-wrap-brbr-in-a-span-using-js-jquery%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
This may help: stackoverflow.com/questions/3337587/…
– Blake F.
Nov 13 '18 at 15:41
3
Couldn't you use css rule instead?
brline-height:14px;
?– charlietfl
Nov 13 '18 at 15:41
Why wrap them in a span ? maybe there is another solution for your problem
– Mihai T
Nov 13 '18 at 15:48
You don't need to wrap
br
elements in aspan
just to setline-height
on them, do it directly in CSS– Rory McCrossan
Nov 13 '18 at 15:51
@charlietfl @rorymccrossan the OP is probably confronted to an existing content where
<br><br>
is legion and he wants to treat only this case without changing anything to the existing single<br>
tags.– Laurent S.
Nov 13 '18 at 16:06