Tarek Saied
Tarek Saied

Reputation: 6616

need help understanding this code

this code in book jQuery in action page 131

i don't understand

.trigger('adjustName');

what is adjustName

and Simple explanation for trigger()

thanks :)

$('#addFilterButton').click( function() {
    var filterItem = $('<div>')
    .addClass('filterItem')
    .appendTo('#filterPane')
    .data('suffix','.' + (filterCount++));
    $('div.template.filterChooser')
    .children().clone().appendTo(filterItem)
    .trigger('adjustName');
});

Upvotes: 0

Views: 154

Answers (3)

MillsJROSS
MillsJROSS

Reputation: 1239

It allows you to trigger, or run, an event. For instance if you wanted the code to mimic the clicking of a button, you could write....

$("#myButton").trigger('click');

This would then run exactly as if you had clicked the button yourself.

'adjustName' is a custom event. So the trigger function is running that custom event. The custom event is assigned using the jQuery bind function.

$("#someElement").bind('adjustName', function() {/* Some Code */});

You might create a customer event for clarity. Perhaps your application opens a document, so you might want an event called 'openDocument' and 'closeDocument' assigned to the element containing the document.

Upvotes: 1

Felix Kling
Felix Kling

Reputation: 816404

It is a string, the name of a custom event you defined.

E.g. it would trigger the event handler bound by:

el.bind('adjustName', function(){...});

For more information I suggest to have a look at the documentation:

Any event handlers attached with .bind() or one of its shortcut methods are triggered when the corresponding event occurs. They can be fired manually, however, with the .trigger() method. A call to .trigger() executes the handlers in the same order they would be if the event were triggered naturally by the user.


Without knowing the context of the code, I would say that calling .trigger() here has no effect, as it is called on the cloneed elements and the event handlers are only cloned if true is passed to clone.

Upvotes: 3

RRStoyanov
RRStoyanov

Reputation: 1172

Maybe the original jQuery manual could be helpful?

Description: Execute all handlers and behaviors attached to the matched elements for the given event type.

Upvotes: 1

Related Questions