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?

3
  • Possible repeat: stackoverflow.com/questions/2145988/string-replace-in-jquery Commented Feb 8, 2013 at 15:30
  • try this: $('#ValidationSummary1').replace('<br /><br />', '<br />'); Commented Feb 8, 2013 at 15:39
  • @Asdfg - jQuery itself doesn't have a .replace() function, which is why I posted the question. Commented Feb 8, 2013 at 15:41

4 Answers 4

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 />'));
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

0
 var $val= $('#ValidationSummary1');
 $val.html($val.html().replace(/[<br>]+$/, '<br/>');

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.