I have problem with remove()
method. I can't remove $(this)
object. My code is:
$(".submit_add_type").live("click", function() {
var parent = $(this).parent();
var type_value = parent.children('.type_value').val();
var type_name = parent.children('.type_name').val();
var parent2 = parent.parent();
var permission = parent2.attr('id').replace('perm_types_', '');
$.ajax({
url: "/admin/ajax/permission_type_add", type: "POST", cache: false,
data: {
permission: permission,
type_value: type_value,
type_name: type_name,
},
success: function(text) {
if(text.substr(0,2) == "ok") {
var id = text.replace('ok|', '');
$(this).remove();
parent.append('<input class="submit" type="submit" name="" value="edytuj" id="edit_type_'+ id +'" /> <input class="submit delete_type" type="submit" name="" value="usuń" id="delete_type_'+ id +'" />')
} else {
alert(text);
}
}
});
});
When I change $(this).remove();
on $(".submit_add_type").remove();
it works perfectly. What could be wrong?
The thing is, there can be other objects with this submit_add_type
class and I want to remove only a particular one.
Upon entering the success
function, $(this)
becomes the text
argument. Change it to something like:
$(".submit_add_type").live("click", function() {
var _this = $(this);
...
$(_this).remove();
...
You may have some other issue going on there, when I try it out in jsFiddle, it works just fine: http://jsfiddle.net/uLVR5/
It looks like you have things happening before the .remove() call, is it a substantial amount?
Your problem is that the value of this
changes inside the $.ajax
callback. You need to save this
as a variable and then use that inside the callback.
var that = this;
$.ajax({
...
success: function(){
$(that).remove();
}
});
"this" is refering to the object "{url:"...", data:"..."}" so it does not make sense to have "$(this).remove()" where you put it.
Use:
$(".submit_add_type").live("click", function() {
var elem= $(this), parent = elem.parent();
var type_value = parent.children('.type_value').val();
var type_name = parent.children('.type_name').val();
var parent2 = parent.parent();
var permission = parent2.attr('id').replace('perm_types_', '');
$.ajax({
url: "/admin/ajax/permission_type_add", type: "POST", cache: false,
data: {
permission: permission,
type_value: type_value,
type_name: type_name,
},
success: function(text) {
if(text.substr(0,2) == "ok") {
var id = text.replace('ok|', '');
elem.remove();
parent.append('<input class="submit" type="submit" name="" value="edytuj" id="edit_type_'+ id +'" /> <input class="submit delete_type" type="submit" name="" value="usuń" id="delete_type_'+ id +'" />')
} else {
alert(text);
}
}
});
});