Ad
Calculate The Children In The Parent Div And Removing Each Child By Clicking On The Button Untill Last Child
I am struggling to remove the child div's except last one in each parent div with same class, my code structure is as below with my Java script to remove child div name repeating-div :
$('.repeatable').on('click', '.close-repeating-div', function(e) {
e.preventDefault();
var countdiv = $('.repeatable').children('.repeating-div').length;
if (countdiv > 1) {
$(this).closest('.repeating-div').remove();
}
});
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js" integrity="sha384-a5N7Y/aK3qNeh15eJKGWxsqtnX/wWdSZSKp+81YjTmS15nvnvxKHuzaWwXHDli+4" crossorigin="anonymous"></script>
<link rel="stylesheet" target="_blank" rel="nofollow noreferrer" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">
<div class="repeatable">
<div class="repeating-div">
<input type="text" class="form-control">
<a target="_blank" rel="nofollow noreferrer" href="#" class="btn close-repeating-div form-control"><i class="fa fa-remove" aria-hidden="true"></i></a>
</div>
<div class="repeating-div">
<input type="text" class="form-control">
<a target="_blank" rel="nofollow noreferrer" href="#" class="btn close-repeating-div form-control"><i class="fa fa-remove" aria-hidden="true"></i></a>
</div>
<div class="repeating-div">
<input type="text" class="form-control">
<a target="_blank" rel="nofollow noreferrer" href="#" class="btn close-repeating-div form-control"><i class="fa fa-remove" aria-hidden="true"></i></a>
</div>
</div>
<div class="repeatable">
<div class="repeating-div">
<input type="text" class="form-control">
<a target="_blank" rel="nofollow noreferrer" href="#" class="btn close-repeating-div form-control"><i class="fa fa-remove" aria-hidden="true"></i></a>
</div>
<div class="repeating-div">
<input type="text" class="form-control">
<a target="_blank" rel="nofollow noreferrer" href="#" class="btn close-repeating-div form-control"><i class="fa fa-remove" aria-hidden="true"></i></a>
</div>
<div class="repeating-div">
<input type="text" class="form-control">
<a target="_blank" rel="nofollow noreferrer" href="#" class="btn close-repeating-div form-control"><i class="fa fa-remove" aria-hidden="true"></i></a>
</div>
</div>
Here I want to remove child div's if there are more than one in each parent separately but my code is calculating all the child div's in one stream but I need to calculate them separately and need to remove them if there are more than one child div in each parent div
Ad
Answer
You need to specify that you are seaching only in the current .repeatable
:
var countdiv = $(this).closest('.repeatable').children('.repeating-div').length;
The above code means you start from the element that triggered your event (.close-repeating-div
), go up to its .repeatable
parent, then count only this div
's children.
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad