Sam Sam - 1 year ago 88
jQuery Question

How to remove the next element after removal of parent in Jquery

I have simplified my problem to the following markup:

<div id="A">
<div id="B"></div>
<div id="C"></div>

And JQuery JS as in:

$('#B').click(function() {

It removes A and thats what I want. But I also want to remove the br tag that immediately follows the parent. I tried various combinations of parent(), first(), next() of parent(), parent().parent(), etc... No luck.

Answer Source

You should use jQuery .is() check to check if the next element is a
and then remove it. Refer Below code and fiddle :

$(document).ready(function() {
  $('#B').click(function() {
    if($(this).parent().next().is('br')) {

JsFiddle :

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download