0

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?

Jon Harding
  • 4,928
  • 13
  • 51
  • 96

4 Answers4

14

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 />'));
PinnyM
  • 35,165
  • 3
  • 73
  • 81
0

JavaScript itself does have a replace function, and given that jQuery is a framework built upon JavaScript, you do have that method available. So, this means you'll have to do a get-manipulate-set process instead of perhaps some nice jQuery method chaining to do it in place.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
0
 var $val= $('#ValidationSummary1');
 $val.html($val.html().replace(/[<br>]+$/, '<br/>');

whatever the number of
they will be replaced by one
(better html)

mikakun
  • 2,203
  • 2
  • 16
  • 24
0

In your case (need to remove html tags) the best solution is to use .text() instead of .html(). The difference is that .html() keeps html tags, and .text() keeps only text (removes any html).

For example:

var value1 = $('.some_div').html(); //returns text content with HTML tags
var value2 = $('.some_div').text(); //returns only text content
Sergey Fedirko
  • 659
  • 1
  • 7
  • 17