this.remove() is not a function. How come?
var vehicle = function () {
return {
init: function () {
jQuery('.vehicle-year-profile .options .delete').bind('click', function (e) {
e.preventDefault();
this.remove();
});
},
remove: function () {
alert('test');
}
}
}();
jQuery().ready(vehicle.init);
Sorry for the confusion. I'm trying to call my own "remove" function. This is simply a class to manage vehicles on my page. This is the beginning of it and it will have a lot more functions than just init/remove.
This is a standard JavaScript error when trying to call a function before it is defined. This error occurs if you try to execute a function that is not initialized or is not initialized correctly. This means that the expression did not return a function object.
Nested functions A function is called “nested” when it is created inside another function. It is easily possible to do this with JavaScript. Here the nested function getFullName() is made for convenience. It can access the outer variables and so can return the full name.
Approach: Write one function inside another function. Make a call to the inner function in the return statement of the outer function. Call it fun(a)(b) where a is parameter to outer and b is to the inner function.
this
is a DOM element. To use jQuery's .remove()
method, you need to wrap it in a jQuery object.
$(this).remove();
EDIT: If you were hoping to call the remove()
function in the vehicle
object, then call:
vehicle.remove();
Also, if you were hoping to shorten your .ready()
call, you can do this:
jQuery(vehicle.init);
From the jQuery 1.4 release notes:
The
jQuery().ready()
technique still works in 1.4 but it has been deprecated. Please use eitherjQuery(document).ready()
orjQuery(function(){})
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With