samC samC - 1 month ago 14
CSS Question

jQuery hide() Cannot get 'duration' setting to be recognized

Can't seem to find an answer to this.

I am attempting to animate the

hide()
when hiding a
<li>
element. I have a
<span>
inside of the
<li>
that acts as a 'delete' button.

This is the part I can't seem to get working.

$('#list1').on('click', 'li span', function(event) {
$(this).parent().hide(1000);
});


The list item IS, in fact, being hidden. The duration, however, is NOT taking effect. The item is disappearing without any sort of 'animation'.

Here it is in action: https://jsfiddle.net/scampione/effnuct0/14/

Would appreciate any help.

Answer

Seems to work fine here once I included jQuery.

As noted in the comments, jQuery Slim does not contain the animation you're using. You need to include the full jQuery library.

var buildArray = function(max) {
  var rtnArray = [];
  var i = 0;
  for (i = 1; i <= max; i++) {
    rtnArray.push(i);
  }
  return rtnArray;
}

var feedBack = $('.display-feedback');

var delButton = '<span class="delete-item">X</span>';

var appendToList = function(theStr) {
  var target = $('#list1')
  target.append('<li>' + theStr + delButton + '</li>')
}


$('.clear-feedback').on('click', function() {
  feedBack.empty();
});

$('#list1').on('click', 'li span', function(event) {
  $(this).parent().hide(1000);
});

$('#list1').on('click', 'li', function(event) {
  feedBack.text(event.type + " - " + event.target.nodeName + " - " + event.currentTarget);
});

var myArray = buildArray(10);
appendToList(myArray)
appendToList(myArray.map(num => num * 7));
appendToList(myArray.filter(num => num > 5));
appendToList(myArray.map(num => num * 7).filter(num => num > 35));
.container {
  height: auto;
  border: solid 1px cornflowerblue;
  border-radius: 5px;
  padding: 10px;
  margin-bottom: 30px;
  font-family: Arial;
  color: #717171;
}
.output-list {
  list-style: none;
  background-color: #ffffff;
  padding: 5px;
  border: solid 2px #77ddbb;
  border-radius: 5px;
}
.output-list li {
  display: flex;
  justify-content: space-between;
  background-color: #44bbee;
  border-radius: 5px;
  color: white;
  padding: 5px;
  margin: 5px;
}
.output-list > span {
  display: inline-block;
  padding: 5px;
  margin: 5px;
  border: solid 1px #a0a0a0;
  border-radius: 5px;
}
.delete-item {
  color: #e0e0e0;
  padding: 3px;
  border-radius: 5px;
}
.delete-item:hover {
  color: #ffffff;
  cursor: pointer;
}
.feedback {
  display: flex;
  justify-content: space-between;
  align-content: flex-start;
}
.clear-feedback {
  width: 39px;
  height: 20px;
  padding: 3px;
  border-radius: 5px;
  text-align: center;
  background-color: #aabbcc;
  color: white;
}
.clear-feedback:hover {
  background-color: #778899;
}
.clear-feedback:active {
  background-color: #556677;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <ul class="output-list" id='list1'>
    <span>a &ltspan&gt in list 1</span>
    <li>this &ltLI&gt is from HTML
      <span class='delete-item'>X</span>
    </li>
  </ul>


  <div class="feedback">
    <div class='display-feedback'></div>
    <span class='clear-feedback'>Clear</span>
  </div>
</div>

Comments