Jon Harding Jon Harding - 5 months ago 39
jQuery Question

How do I replace “<br /><br />” with “<br />” in HTML using JavaScript and jQuery?

I have validation summary that sometimes has multiple

br
tags at the end like below:

<div id="ValidationSummary1" class="alert alert-error">
Last 4-digits of Social Security number does not match
the Member record on file for the Member ID number you
have entered.<br />
Date of Birth does not match the Member record on file for the Member ID
number you have entered.<br /><br />
</div>


I'm trying to find anytime there are two
br
tags back to back and replace it with one like below:

$('#ValidationSummary1').replace('<br /><br />', '<br />');


I understand there isn't a
replace()
function. What would be the alternative to something simple like this?

Answer

Use .html() to get and set the content passing it through the standard JS replace().

var div = $('#ValidationSummary1');
div.html(div.html().replace('<br /><br />', '<br />'));