Reputation: 2512
Seems the following code:
$("#logo-events").trigger("remove");
Does same thing as
$("#logo-events").remove();
Is that expected behavior? You can try it at jquery website opening a console.
Upvotes: 4
Views: 1194
Reputation: 1111
I included the trigger function for your reference, but yes that is the expected behavior. Trigger allows you to manually (or not) execute an event such as remove in your case. This would normally be for support on a "on()" function. For example
( "#logo-events" ).on( "click", function() {
alert( $( this ).text() );
});
$( "#logo-events" ).trigger( "click" );
This block manually triggers a click function, but what it doesn't say is that if you just run a trigger on an element with no event, yet a function it just executes the function.
for example:
$("logo-events").trigger("remove");
The following will just run.
Hope that helps.
Upvotes: -1
Reputation: 21911
From the documentation of the .trigger()
method:
For both plain objects and DOM objects other than window, if a triggered event name matches the name of a property on the object, jQuery will attempt to invoke the property as a method if no event handler calls event.preventDefault(). If this behavior is not desired, use .triggerHandler() instead
.remove()
is a native function of the DOM:
The remove() method, when invoked, must run these steps:
- If context object’s parent is null, terminate these steps.
- Remove the context object from context object’s parent.
Combine these two and you get the observed behaviour.
When you trigger "remove" jQuery calls the native .remove()
method of the node which then removes the element.
The relevant part in jQuery/event.js:
if ( ontype && jQuery.isFunction( elem[ type ] ) && !jQuery.isWindow( elem ) ) {
// ...
elem[ type ](); // elem["remove"]();
// ...
}
Upvotes: 5
Reputation: 1813
.trigger() is simply a way of triggering an action upon load, whereas .remove() is a method that can be placed within a triggered action (whether automatic or manual). If you are using .trigger('remove') then there seems no point in having that element in the first place.
Upvotes: -1