0

I was playing with jsfiddle here: http://jsfiddle.net/dm9eebz9/

HTML

<div id="wrapper">
    <p class="body">Line 1
        <br>
        <br>Line 3</p>
    <p class="body">Line 1
        <br>Line 2
        <br>
        <br>
        <br>
        <br>Line 6</p>
    <p class="body">Line 1
        <br>Line 2</p>
</div>

Javascript/jQuery

$('#wrapper p.body').each(function () {
    var temp = $(this).innerHTML;
    this.innerHTML = temp.replace(/(<br>)+/gim, "<br>");
});

My goal was to try to get rid of more than 2 <br>'s when they occur. However, it doesn't seem to recognize temp. Is this jsfiddle behavior, or am I making an obvious error?

6
  • I don't think there's an innerHTML property on a jQuery array. Commented Sep 5, 2014 at 13:39
  • 1
    Read these articles to learn how to debug JavaScript, so you can and help yourself. Commented Sep 5, 2014 at 13:40
  • What exactly is the final result? Do you want to remove the br if there are more than 2 consecutive br nodes? I.e. it would remove the last two br in the second paragraph? Commented Sep 5, 2014 at 13:56
  • if you want to access javascript functions off of jQuery, you need to do this $(this)[0] and it gives you back that raw javascript. Commented Sep 5, 2014 at 14:13
  • @No1_Melman: Don't confuse JavaScript with DOM. $(this)[0] returns a DOM element which lets you access the DOM API. Of course you are using JavaSCript, but jQuery is JavaScript as well. Commented Sep 5, 2014 at 15:39

4 Answers 4

1

The jsfiddled: http://jsfiddle.net/pe6w7z1e/1/

The idea is: there will be one break and one/more (one/more spacenewline + break) >> replace with just one break

Here is the modified code:

$('p.body').each(function () {
    var elem = $(this)
    var temp = elem.html();
    elem.html(temp.replace(/<br>(\s+<br>)+/img, "<br>"));

});

Please mark as answer if it answers your question

I think it is better to use jquery all cases. Mixing of Jquery and raw javascript is not good concept.

Sign up to request clarification or add additional context in comments.

5 Comments

.replace is a javascript method, not jquery
Thanks Ivo for your comments. Could you show any reference? What's the alternative for jquery?
"What's the alternative for jquery? " There is none because jQuery is not a library for text processing.
Ah then we can only use replace here.. Thanks Felix
I'd use either jquery or javascript only, but the system I am working in (spiceworks) seems to have some major issues with certain things.
1

Use .html() not innerHTML;

The regex pattern /(<br>\s*)+/ takes white space into account with \s* which matches 0 or more white space characters.

$('p.body').each(function () {
    var temp = $(this).html();
    $(this).html(temp.replace(/(<br>\s*)+/gim, "<br>"));
});

Also as commented, use your browsers console for debugging and always RTFM.

Fiddle: http://jsfiddle.net/dm9eebz9/4/

Comments

1

I really don't think regular expressions are the right tool for this. You can use a combination of .contents and .filter to remove the duplicate <br /> tags:

var prev = null;
var $nodes = $('.body')
    .contents()
    .filter(function () {
        return this.nodeType !== 3 || $.trim(this.data) !== "";
    });

for (var i = 1; i < $nodes.length; i++) {
    if ($nodes.eq(i).is('br') && $nodes.eq(i-1).is('br')) {
        $nodes.eq(i).remove();
    }
}

The complexity here is the text nodes in between the <br /> tags, thus the two-step process.

Example: http://jsfiddle.net/dm9eebz9/9/

Comments

0

You need to use the correct object for the innerHTML property. That is a DOM object property and not a jQuery one. You can either use the innerHTML DOM property on the DOM element:

$('p.body').each(function () {
    var temp = this.innerHTML;
    this.innerHTML = temp.replace(/(<br>)+/gim, "<br>");
});

JSFiddle: http://jsfiddle.net/dm9eebz9/6/

or use the html() method on a jQuery object:

$('p.body').each(function () {
    var temp = $(this).html();
    this.html(temp.replace(/(<br>)+/gim, "<br>"));
});

http://jsfiddle.net/dm9eebz9/7/

Note a "correct" line break is <br/>, not <br> but I gather you cannot change that

2 Comments

<br/> cannot be used, as the pages I am editing have <br>, correct or not.
Was updating the JSFiddle, but I am dumping this answer as the other answer is far simpler :)

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.