I have an disappearing delete animation code and I want to get the entire div "parent_parent" to disappear.
Here is the HTML
<div class="parent_parent">
<div class="parent">
<a href="?delete=1" class="delete_link"></a>
</div>
</div>
And here is part of the jquery code that makes the parent_parent div disappear:
$(document).ready(function() {
$('a.delete_element').click(function(e) {
e.preventDefault();
var parent = $(this).parent();
$.ajax({
type: 'get',
url: 'delete.php',
data: 'ajax=1&delete=' + parent.parent().attr('id').replace('sort_', ''),
beforeSend: function() {
parent.parent().animate({
'backgroundColor': '#fff'
}, 300);
},
success: function() {
parent.parent().slideUp(300,function() {
parent.parent().remove();
});
}
});
});
});
But so far no animation happens, but if I just call one parent then the inside div does disappear. I don't get any error messages either.
parent
isn't defined. You need $(this)
, and then a reference to what you want - Chris Laplante 2012-04-05 02:13
$(this).parent().parent().remove();
should be $(this).remove()
, just as I had it in my cod - Chris Laplante 2012-04-05 02:16
div
its own ID. That way, you don't need to mess with parent()
and classes - Chris Laplante 2012-04-05 02:25
delete_link
structures on your page - Chris Laplante 2012-04-05 02:34
Your code is still too complicated for what you are trying to do. This is better:
// $(function(){ is shorthand for $(document).ready(function(){
$(function() {
$('a.delete_element').click(function(e) {
e.preventDefault();
// Don't give thing ambiguous names like 'parent'
// You should change your class names too.
var container = $(this).closest(".parent_parent");
$.ajax({
type: 'get',
url: 'delete.php',
// You had multiple instances of parent.parent(), but no direct usage of parent alone
// This is a clue that you can simplify parent.parent() since you are traversing to the same element every time
data: 'ajax=1&delete=' + container.attr('id').replace('sort_', ''),
beforeSend: function() {
containter.animate({
'backgroundColor': '#fff'
}, 300);
},
success: function() {
container.slideUp(300, function() {
// Do not repeat the same selector within a callback
// That's what `this` is for
$(this).remove();
});
}
});
});
});
If you use this code example as it is, it will work.
success
is probably a callback on an Ajax operation. Please post the code that uses success
, since that will determine what the first this
refers t - Chris Laplante 2012-04-05 02:18
parent
, though at this point I'm not sure what it refers to - Chris Laplante 2012-04-05 02:30
You are probably not preventing the default anchor tag action. You probably also want to cache references that you're going to use multiple times. Here's working code:
function(e) {
e.preventDefault();
var theParent = $(this).closest(".parent_parent");
theParent.slideUp(300, function() {
theParent.remove();
});
};
Fiddle: http://jsfiddle.net/VXEUM/
Note also that I'm using closest()
instead of doubling up on parent()
. Just a style preference. Plus if your element gets nested more deeply it will still work using closest()
.
.
between the lastparent()
andslideUp
- Loktar 2012-04-05 02:11