control-freak-ide/Control-Freak-Documentation/jQuery/docs/entries/trigger.json
plastic-hub-dev-node-saturn 538369cff7 latest
2021-05-12 18:35:18 +02:00

1 line
5.7 KiB
JSON

{"name":"trigger","type":"method","title":".trigger()","deprecated":null,"removed":null,"desc":"Execute all handlers and behaviors attached to the matched elements for the given event type.","categories":["events/event-handler-attachment","version/1.0"],"entries":[{"return":"jQuery","signatures":[{"added":"1.0","argument":[{"desc":"A string containing a JavaScript event type, such as <code>click</code> or <code>submit</code>.","name":"eventType","type":"String"},{"type":[{"name":"Array"},{"name":"PlainObject"}],"desc":"Additional parameters to pass along to the event handler.","name":"extraParameters","optional":"true"}]},{"added":"1.3","argument":[{"desc":"A <ahref=\"/category/events/event-object/\"><code>jQuery.Event</code></a> object.","name":"event","type":"Event"},{"type":[{"name":"Array"},{"name":"PlainObject"}],"desc":"Additional parameters to pass along to the event handler.","name":"extraParameters","optional":"true"}]}],"examples":[{"desc":"Clicks to button #2 also trigger a click for button #1.","code":"\n$( \"button:first\" ).click(function() {\n update( $( \"span:first\" ) );\n});\n\n$( \"button:last\" ).click(function() {\n $( \"button:first\" ).trigger( \"click\" );\n update( $( \"span:last\" ) );\n});\n\nfunction update( j ) {\n var n = parseInt( j.text(), 10 );\n j.text( n + 1 );\n}\n","css":"\n button {\n margin: 10px;\n }\n div {\n color: blue;\n font-weight: bold;\n }\n span {\n color: red;\n }\n","html":"\n<button>Button #1</button>\n<button>Button #2</button>\n<div><span>0</span> button #1 clicks.</div>\n<div><span>0</span> button #2 clicks.</div>\n"},{"desc":"To submit the first form without using the submit() function, try:","code":"\n$( \"form:first\" ).trigger( \"submit\" );\n"},{"desc":"To submit the first form without using the submit() function, try:","code":"\nvar event = jQuery.Event( \"submit\" );\n$( \"form:first\" ).trigger( event );\nif ( event.isDefaultPrevented() ) {\n // Perform an action...\n}\n"},{"desc":"To pass arbitrary data to an event:","code":"\n$( \"p\" )\n .click(function( event, a, b ) {\n // When a normal click fires, a and b are undefined\n // for a trigger like below a refers to \"foo\" and b refers to \"bar\"\n })\n .trigger( \"click\", [ \"foo\", \"bar\" ] );\n"},{"desc":"To pass arbitrary data through an event object:","code":"\nvar event = jQuery.Event( \"logged\" );\nevent.user = \"foo\";\nevent.pass = \"bar\";\n$( \"body\" ).trigger( event );\n"},{"desc":"Alternative way to pass data through an event object:","code":"\n$( \"body\" ).trigger({\n type:\"logged\",\n user:\"foo\",\n pass:\"bar\"\n});\n"}],"longdesc":"\n <p>Any event handlers attached with <code>.on()</code> or one of its shortcut methods are triggered when the corresponding event occurs. They can be fired manually, however, with the <code>.trigger()</code> method. A call to <code>.trigger()</code> executes the handlers in the same order they would be if the event were triggered naturally by the user:</p>\n <pre><code>\n$( \"#foo\" ).on( \"click\", function() {\n alert( $( this ).text() );\n});\n$( \"#foo\" ).trigger( \"click\" );\n </code></pre>\n <p>As of jQuery 1.3, <code>.trigger()</code>ed events bubble up the DOM tree; an event handler can stop the bubbling by returning <code>false</code> from the handler or calling the <a href=\"/event.stopPropagation/\"><code>.stopPropagation()</code></a> method on the event object passed into the event. Although <code>.trigger()</code> simulates an event activation, complete with a synthesized event object, it does not perfectly replicate a naturally-occurring event.</p>\n <p>To trigger handlers bound via jQuery without also triggering the native event, use <a href=\"/triggerHandler/\"><code>.triggerHandler()</code></a> instead. </p>\n <p>When we define a custom event type using the <code>.on()</code> method, the second argument to <code>.trigger()</code> can become useful. For example, suppose we have bound a handler for the <code>custom</code> event to our element instead of the built-in <code>click</code> event as we did above:</p>\n <pre><code>\n$( \"#foo\" ).on( \"custom\", function( event, param1, param2 ) {\n alert( param1 + \"\\n\" + param2 );\n});\n$( \"#foo\").trigger( \"custom\", [ \"Custom\", \"Event\" ] );\n </code></pre>\n <p>The event object is always passed as the first parameter to an event handler. An array of arguments can also be passed to the .trigger() call, and these parameters will be passed along to the handler as well following the event object. As of jQuery 1.6.2, single string or numeric argument can be passed without being wrapped in an array.</p>\n <p>Note the difference between the extra parameters passed here and the <code>eventData</code> parameter to the <a href=\"/on/\">.on()</a> method. Both are mechanisms for passing information to an event handler, but the <code>extraParameters</code> argument to <code>.trigger()</code> allows information to be determined at the time the event is triggered, while the <code>eventData</code> argument to <code>.on()</code> requires the information to be already computed at the time the handler is bound.</p>\n <p>The <code>.trigger()</code> method can be used on jQuery collections that wrap plain JavaScript objects similar to a pub/sub mechanism; any event handlers bound to the object will be called when the event is triggered. </p>\n <div class=\"warning\"><strong>Note:</strong> For both plain objects and DOM objects other than <code>window</code>, 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 <code>event.preventDefault()</code>. If this behavior is not desired, use <code>.triggerHandler()</code> instead.</div>\n "}]}