user3653474
user3653474

Reputation: 3854

How to remove only first set of div of a specific class jquery

I have html structure like this :

  <div class="row clearfix student">
          <div class="col-sm-3">
            <div class="form-group">
              <h2 class="card-inside-title">Student</h2>
              <div class="form-line focused">
                <input type="text" name="name" class="form-control" required="">
              </div>
            </div>
          </div>
 <div class="col-sm-3">
            <div class="form-group">
            <h2 class="card-inside-title">Subject</h2>
            <select name="subject_name" class="form-control subject" required="" aria-invalid="false"> 
                                        <option value="">Select</option>
                                        <option value="1">Maths</option>
                                        <option value="2">Science</option>
          </select>
        </div>
        </div>
        <div>
        
        <div class="row clearfix subject">
          <div class="col-sm-3">
            <div class="form-group">
              <h2 class="card-inside-title">Subject</h2>
              <div class="form-line focused">
                <input type="text" name="subject[]" class="form-control" required="">
              </div>
            </div>
          </div>
        <div>
        
        <div class="row clearfix class">
          <div class="col-sm-3">
            <div class="form-group">
              <h2 class="card-inside-title">Subject</h2>
              <div class="form-line focused">
                <input type="text" name="subject[]" class="form-control" required="">
              </div>
            </div>
          </div>
        <div>
        
        
        <div class="row clearfix student">
          <div class="col-sm-3">
            <div class="form-group">
              <h2 class="card-inside-title">Student</h2>
              <div class="form-line focused">
                <input type="text" name="name" class="form-control" required="">
              </div>
            </div>
          </div>

<div class="col-sm-3">
            <div class="form-group">
            <h2 class="card-inside-title">Subject</h2>
            <select name="subject_name" class="form-control subject" required="" aria-invalid="false"> 
                                        <option value="">Select</option>
                                        <option value="1">Maths</option>
                                        <option value="2">Science</option>
          </select>
        </div>
        </div>
        <div>
        <div>
        
        <div class="row clearfix subject">
          <div class="col-sm-3">
            <div class="form-group">
              <h2 class="card-inside-title">Subject</h2>
              <div class="form-line focused">
                <input type="text" name="subject[]" class="form-control" required="">
              </div>
            </div>
          </div>
        <div>
        
        <div class="row clearfix class">
          <div class="col-sm-3">
            <div class="form-group">
              <h2 class="card-inside-title">Subject</h2>
              <div class="form-line focused">
                <input type="text" name="subject[]" class="form-control" required="">
              </div>
            </div>
          </div>
        <div>
        ..
        ..

There is a student row and multiple subject row.There is a dropdown on the Student row, so what i want is to remove the subject rows only when dropdown is changed. I'm using this code but it is not working.

jQuery(document).on('change', '.subject', function(e) {
  var nearest_row = $(this).closest('div.row');
  $(nearest_row).next().remove();
});

I don't want to delete student, only the subjects rows of that student.

Upvotes: 0

Views: 49

Answers (1)

I believe the problem is that you have lots of unclosed <div> in your code, the each row is actually inside each other. If you look at the example below, i have closed the divs, and now it seems to work.

Working demo

jQuery(document).on('change', 'select.subject', function(e) {
  var nearest_row = $(this).closest('div.row');
  if ($(nearest_row).next('.row').find("select").length == 0) {
    $(nearest_row).next().remove();
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row clearfix student">
  <div class="col-sm-3">
    <div class="form-group">
      <h2 class="card-inside-title">Student</h2>
      <div class="form-line focused">
        <input type="text" name="name" class="form-control" required="">
      </div>
    </div>
  </div>
</div>
<div class="row clearfix subject">
  <div class="col-sm-3">
    <div class="form-group">
      <h2 class="card-inside-title">Subject</h2>
      <select name="subject_name" class="form-control subject" required="" aria-invalid="false">
        <option value="">Select</option>
        <option value="1">Maths</option>
        <option value="2">Science</option>
      </select>
    </div>
  </div>
</div>
<div class="row clearfix subject">
  <div class="col-sm-3">
    <div class="form-group">
      <h2 class="card-inside-title">Subject</h2>
      <div class="form-line focused">
        <input type="text" name="subject[]" class="form-control" required="">
      </div>
    </div>
  </div>
</div>

<div class="row clearfix class">
  <div class="col-sm-3">
    <div class="form-group">
      <h2 class="card-inside-title">Subject</h2>
      <div class="form-line focused">
        <input type="text" name="subject[]" class="form-control" required="">
      </div>
    </div>
  </div>
</div>


<div class="row clearfix student">
  <div class="col-sm-3">
    <div class="form-group">
      <h2 class="card-inside-title">Student</h2>
      <div class="form-line focused">
        <input type="text" name="name" class="form-control" required="">
      </div>
    </div>
  </div>
</div>
<div class="row clearfix subject">
  <div class="col-sm-3">
    <div class="form-group">
      <h2 class="card-inside-title">Subject</h2>
      <select name="subject_name" class="form-control subject" required="" aria-invalid="false">
        <option value="">Select</option>
        <option value="1">Maths</option>
        <option value="2">Science</option>
      </select>
    </div>
  </div>
</div>

<div class="row clearfix subject">
  <div class="col-sm-3">
    <div class="form-group">
      <h2 class="card-inside-title">Subject</h2>
      <div class="form-line focused">
        <input type="text" name="subject[]" class="form-control" required="">
      </div>
    </div>
  </div>
</div>

<div class="row clearfix class">
  <div class="col-sm-3">
    <div class="form-group">
      <h2 class="card-inside-title">Subject</h2>
      <div class="form-line focused">
        <input type="text" name="subject[]" class="form-control" required="">
      </div>
    </div>
  </div>
</div>

Upvotes: 1

Related Questions