Input type text of the event. Form events. Creating a custom event

The input event fires when the value of an element is used to create interactive controls for web-based forms in order to accept data from the user; a wide variety of types of input data and control widgets are available, depending on the device and user agent. "\u003e , element represents a control that provides a menu of options "\u003e ...

An example that shows how to use the change event to get the value of the selected input element with type equal to radio:

Windows Linux macOS ...

jQuery - Select Event

The select event is generated by the browser when the user, inside input elements with type \u003d "text" or textarea, selects text.

$ ("# target"). select (function () (console.log ("Select event handler called");));

jQuery - Form submit event

The submit event is raised on an element when the user tries to submit a form. This event can only be added to form elements.

Example, using the submit event:

...
...

A programmatic call to submit the form:

$ ("# feedback"). submit (); $ ("# feedback"). trigger ("submit");

jQuery - Scroll Event

JQuery uses the scroll event to keep track of the scroll state.

For example, let's hang on the page scroll event a function that will display an element with the scrollup class if the scroll value is greater than 200px and hide it if the scroll value is less than this value.

// shorthand record of the function $ (window) .scroll (function () (// actions when scrolling the page ... if ($ (this) .scrollTop ()\u003e 200) ($ (". scrollup"). fadeIn () ;) else ($ (". scrollup"). fadeOut ();)));

jQuery - Resize Event

To listen for the browser window change event, use resize:

For example, let's create a handler that will display the width and height of the page at the end of the page when the browser window changes:

$ (window) .resize (function () ($ ("body"). append ("

Width x Height \u003d "+ window.innerWidth +" x "+ window.innerHeight +"

"); });

jQuery - Canceling standard event behavior

Some elements in HTML have standard behavior. For example, when a user clicks on a link, he goes to the address specified in the href attribute. If you do not need this action, you can undo it. To cancel the standard behavior, call the preventDefault method of the event object in the event handler.

For example, let's cancel the standard behavior of all links on the page that have the service class:

$ ("a.service"). on ("click", function (e) (// cancel the default browser action e.preventDefault (); // actions that the link will perform ...));

What is surfacing and how to stop it

In addition to canceling a standard action, there is also such a thing as bubbling in the event mechanism. It lies in the fact that when the browser generates an event, it does it not only for the current element (target), but also for all its descendants, including the parent:

Current element (target) -\u003e parent of target -\u003e grandparent -\u003e ... -\u003e document -\u003e window

There are scenarios in jQuery when, in the presented chain, some element also has a handler for this event, which does not need to be executed. And so that this event does not spread to this element, it must be stopped. To do this, call the stopPropagation method of the event object in the handler of the desired element. After calling this method, the event will stop and will not bubble up.

For example, it is necessary that when the cursor is placed over an element with the mark class, its content turns orange.

Some text ... fragment...... continued ...
...

In this case, if you do not specify the stopPropagation method, then when you move the cursor to a span element with the mark class, this event will occur not only for it, but for all of its parent elements. And this in this example will lead to the fact that the color of not only the text enclosed in the span will change, but also the entire paragraph.

If you need to override the default browser behavior and stop the event bubbling, then in jQuery, instead of calling these two methods, you can simply return false as the function result.

$ ("a"). on ("click", function (e) (//e.preventDefault (); //e.stopPropagation (); ... return false;));

Adding Events to Dynamically Created Objects

In order to hang an event on an element that does not yet exist, you can use the following on function construction:

$ (document) .on (eventName, selector, handler); // document or any other existing parent element // eventName is the name of the event // selector is a selector that filters children for which an event handler should be started // handler is an event handler

This action can be carried out due to the fact that the event floats, and, therefore, occurs in all ancestors of this element. And the object to which all events on the page are floated is document. Therefore, in most cases, it is he who is chosen. After that, knowing the selector, the on function can programmatically select among the elements (the element that caused this event (target) and all its ancestors, including the parent) those that match it. And then for all the selected elements, execute the handler specified in the on function. Actions by which event handling is transferred to another element (ancestor) are called in jQuery else event delegation process.

For example, let's add an event to an element that is not yet on the page:

Delegation can be used not only to handle events of dynamically created objects, but also in order not to bind a handler to each element (if there can be a lot of them on the page).

For example, we prohibit in the comments to go to external links (we will redirect such actions to the away page):

$ (document) .on ("click", "# comment a", function (e) (if (! (location.hostname \u003d\u003d\u003d this.hostname ||! this.hostname.length)) (e.preventDefault ( ); location.href \u003d "away? link \u003d" + encodeURIComponent ($ (this) .attr ("href"));)));

jQuery - Remove Event Handler

Event handlers are removed using the off method. At the same time, using it, you can remove only those handlers that were added through the on method.

Calling the off method without arguments will remove all event handlers added to the specified elements from the specified elements.

For example, let's disable all handlers for elements with the link class:

$ (". link"). off ();

For example, let's remove the click event for all elements with the link class:

$ (". link"). off ("link");

A special selector (**) allows you to remove only delegated events while keeping non-delegated ones:

$ (". link"). off ("click", "**");

Remove only specified delegated events (using a selector):

// add a delegated event $ ("ul"). on ("click", "li", function () (// display the content of the li element console.log ($ (this) .text ());)) ; // remove delegated event $ ("ul"). off ("click", "li");

Remove all openModal handlers for the delegated click event in the modal namespace for elements with class show:

$ ("body"). on ("click.modal", ".show", openModal);

Creating a custom event

JQuery uses the on and trigger methods to create custom events.

Let's look at the principle of creating a custom jQuery event using the following example:

...