Find and wrap in a span using JS/jQuery










0















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?









share|improve this question






















  • 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 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















0















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?









share|improve this question






















  • 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 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













0












0








0








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?









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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

















  • 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 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
















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












2 Answers
2






active

oldest

votes


















2














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...






share|improve this answer




















  • 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


















1














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;






share|improve this answer

























  • 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











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%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









2














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...






share|improve this answer




















  • 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














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...






share|improve this answer




















  • 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








2







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...






share|improve this answer















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>






share|improve this answer














share|improve this answer



share|improve this answer








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












  • 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













1














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;






share|improve this answer

























  • 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















1














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;






share|improve this answer

























  • 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













1












1








1







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;






share|improve this answer















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;







share|improve this answer














share|improve this answer



share|improve this answer








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

















  • 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

















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%2f53284454%2ffind-and-wrap-brbr-in-a-span-using-js-jquery%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

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)