Found 8895 Articles for Front End Technology

How do I prevent jQuery events from bubbling up to parent elements?

Amit D
Updated on 14-Feb-2020 11:05:15

1K+ Views

To prevent jQuery events from bubbling up to parent elements, use the stopPropogation() method. The stopPropagation() method stops the bubbling of an event to parent elements, preventing any parent handlers from being notified of the event.ExampleYou can try to run the following code to prevent jQuery events from bubbling up to parent elements −Live Demo           jQuery stopProagation() method                              $(document).ready(function() {             $("div").click(function(event){                alert("This is : " + $(this).text());                event.stopPropagation();             });          });                              div {             margin:20px;             padding:20px;             border:2px solid #666;             width:160px;          }                             Click on any box to see the effect:                        OUTER BOX                       INNER BOX                            

How to trigger a hover event from another element using jQuery?

Amit D
Updated on 30-Jul-2019 22:30:20

417 Views

Trigger a hover event from another element, using the hover() method over the selected element.You can try to run the following code to learn how to trigger a hover event from another element using jQuery:

How to stop the bubbling of an event to parent elements in jQuery?

Amit D
Updated on 14-Feb-2020 11:04:20

201 Views

To stop the bubbling of an event to parent elements, use the stopPropagation() method. You can try to run the following code to learn how to stop the bubbling of an event to parent elements −ExampleLive Demo           jQuery stopPropagation() method                              $(document).ready(function() {             $("div").click(function(event){                alert("This is : " + $(this).text());                event.stopPropagation();             });          });                              div {              margin:10px;              padding:12px;              border:2px solid #666;              width:160px;          }                             Click on any box to see the effect:                        OUTER BOX                       INNER BOX                                

How to prevent the browser from executing the default action in jQuery?

Amit D
Updated on 14-Feb-2020 10:41:19

673 Views

To prevent the browser from executing the default action in  jQuery, use the preventDefault() method. The preventDefault() method  prevents the browser from executing the default action.ExampleYou can use the method isDefaultPrevented() to know whether this method was ever called (on that event object).Live Demo           jQuery preventDefault() method                              $(document).ready(function() {             $("a").click(function(event){                event.preventDefault();                alert( "Default behavior is disabled!" );             });          });                         Click the following link and it won't work:       GOOGLE Inc.        

How to get attribute of an element when using the 'click' event in jQuery?

Ricky Barnes
Updated on 14-Feb-2020 10:40:25

2K+ Views

To get attribute of an element, use the attr() method in jQuery. You can try to run the following code to get attribute of an element using the ‘click’ event −ExampleLive Demo $(document).ready(function(){     $("button").click(function(){         alert("Width of image: " + $("img").attr("width"));     }); }); Get Width

What are Event Methods in jQuery?

Ricky Barnes
Updated on 14-Feb-2020 10:44:08

158 Views

Commonly used event methods include $(document).ready(), click(), dblclick() etc. There is a list of methods which can be called on an Event Object, The following are some of the methods which can be called on an Event Object, S.NoMethod and Description1preventDefault() Prevents the browser from executing the default action.2isDefaultPrevented() Returns whether event.preventDefault() was ever called on this event object.3isPropagationStopped() Returns whether event.stopPropagation() was ever called on this event object.4stopImmediatePropagation() Stops the rest of the handlers from being executed.Let us see an example of stopPropagation() method. The stopPropagation() method stops the bubbling of an event to parent elements, preventing any parent handlers from being notified ... Read More

What are event attributes in jQuery?

Ricky Barnes
Updated on 11-Dec-2019 06:21:12

384 Views

jQuery events have attributes such as for keyup and keydown events, if the Ctrl key was pressed, timestamp when the event created, etc.The following are some of the event properties/attributes available:S.NoProperty & Description1altKeySet to true if the Alt key was pressed when the event was triggered, false if not. The Alt key is labeled Option on most Mac keyboards.2ctrlKeySet to true if the Ctrl key was pressed when the event was triggered, false if not.3dataThe value, if any, passed as the second parameter to the bind() command when the handler was established.4pageXFor mouse events, specifies the horizontal coordinate of the ... Read More

What is an Event Object in jQuery?

Ricky Barnes
Updated on 11-Dec-2019 06:32:29

264 Views

The callback function takes a single parameter; when the handler is called the JavaScript event object will be passed through it.The event object is often unnecessary and the parameter is omitted, as sufficient context is usually available when the handler is bound to know exactly what needs to be done when the handler is triggered, however there are certain attributes which you would need to be accessed.Let us see an example of isDefaultPrevented() method. The isDefaultPrevented() method checks whether event.preventDefault() was ever called on this event object.ExampleYou can try to run the following code to learn how to work with ... Read More

How to remove event handlers using jQuery?

Ricky Barnes
Updated on 14-Feb-2020 10:37:08

885 Views

Once an event handler is established, it remains in effect for the remainder of the life of the page. There may be a need when you would like to remove event handler.jQuery provides the unbind() command to remove an exiting event handler. The syntax of unbind() is as follows.The following is the description of the parameters −eventType − A string containing a JavaScript event type, such as click or submit. Refer to the next section for a complete list of event types.handler − If provided, identifies the specific listener that's to be removed.ExampleYou can try to run the following code to learn ... Read More

How to check the element type of an event target with jQuery?

Ricky Barnes
Updated on 11-Dec-2019 06:02:58

2K+ Views

To check the element type pf an event target, using the is() method.ExampleYou can try to run the following code to check the element type:Live Demo $(document).ready(function(){    $( "ul" ).click(function( event ) {       var target = $( event.target );       if ( target.is( "li" ) ) {          alert("Element is 'li'")       }   }); }); Click below to find out which element India US UK

Advertisements