-1

html

<p>p</p>
<br>br1
<br>br2
<br>br3

I got 3 times alert instead of 1, why? since

$( function() {
    console.log($('p').text());
    $( 'br' ).each( function() {
        if(this.nextSibling.nodeValue = 'br1'){
            alert('found');
        }
    });
});

what I want to do is delete that line if nodevalue found, like delete <br>br1 if br1 can be found.

user3836151
  • 231
  • 3
  • 5
  • 11

1 Answers1

3

That is because in if you are using = not ==. & you should also check for newLine character. I am assuming \n here.

This line

if(this.nextSibling.nodeValue = 'br1')

should be

if(this.nextSibling.nodeValue == 'br1\n')

As @antyrat told just use remove for that

 $( this.nextSibling ).remove();
 $( this ).remove();

FOR CONTAINS

if(this.nextSibling.nodeValue.indexOf('br')>-1){}
Mritunjay
  • 25,338
  • 7
  • 55
  • 68