Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing two elements in one line

Tags:

jquery

element

Is there a way of completing this script in one line?

$(this).next("br").remove();
$(this).remove();

I tried $(this).remove().next("br").remove(); but this doesn't work because we're removing the element before we can find the next one.

like image 370
Oliver Tappin Avatar asked Mar 06 '13 14:03

Oliver Tappin


3 Answers

You can use addBack() (or its predecessor andSelf() before jQuery 1.8):

$(this).next("br").addBack().remove();

Alternately, you can use end() to get back to the previous set of matched elements:

$(this).next("br").remove().end()
       .remove();
like image 55
Frédéric Hamidi Avatar answered Nov 27 '22 03:11

Frédéric Hamidi


$(this).add( $(this).next("br") ).remove();
like image 27
r043v Avatar answered Nov 27 '22 02:11

r043v


$(this).next("br").addBack().remove();
like image 25
Matt Cain Avatar answered Nov 27 '22 03:11

Matt Cain