How to prevent jQuery .remove() from deleting parent class
Ad
I have created an clone element function which can be viewed view demo here. When the reset button is pressed it removes all cloned item elements, however when attempting to add another item the item list, the 'NEW' added item are not visible with the DOM.
$('#add-btn').on('click',function(){
$('.list-items:first').clone().appendTo("#items").addClass('isVisible');
$('#items-fields').val('');
})
// RESET BUTTON
$('.reset').on('click', function(){
if( $('.list-items').length != 1);
$('.list-items:last').remove();
event.preventDefault();
})
Ad
Answer
Ad
Where you have your reset button change the code in your if statement to the following
$('.reset').on('click', function(){
if($('.list-items').length > 1) {
$('.list-items:last').remove();
}
})
At the moment you have set your list-items the following way..
When a user clicks the delete button, if the number of things with the class list-item does not equal 0, then remove the last list-item
You need to change it the code so it does the following:
When a user clicks the delete button, if the number of things with the class list-item is greater than 1, then remove the last list-item
Ad
source: stackoverflow.com
Related Questions
Ad
- → 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