Found 766 Articles for JQuery

How to trigger the same function with jQuery multiple events?

Amit D
Updated on 11-Dec-2019 07:06:41

4K+ Views

To trigger the same function with multiple events, use the jQuery on() method with multiple events such as click, dblclick, mouse enter, mouse leave, hover, etc.ExampleYou can try to run the following code to learn how to work the same function with jQuery multiple events:Live Demo $(document).ready(function(){     $("p").on({             mouseenter: function(){             $(this).css("background-color", "gray");         },                   mouseleave: function(){             $(this).css("background-color", "red");         },                 dblclick: function(){             $(this).css("background-color", "yellow");         }             }); }); Click, double click and move the mouse pointer.

What is the difference between event.preventDefault() and return false in jQuery?

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

509 Views

Returning false from jQuery event handlers is like calling both preventDefault() and stopPropagation(). The preventDefault() method prevents the browser from executing the default action.ExampleYou can try to run the following code to run event.preventDefault() method in jQuery −Live Demo           jQuery preventDefault() method                              $(document).ready(function() {             $("a").click(function(event){                event.preventDefault();                alert( "Default behavior is disabled!" );             ... Read More

How to check which key has been pressed using jQuery?

Amit D
Updated on 11-Dec-2019 06:42:03

620 Views

To check which key has been pressed, use the onkeydown attribute.ExampleYou can try to run the following code to get which key is pressed:Live Demo Press a key in the below box to get which key is pressed.   function demoFunction(event) {     var a = event.key;     document.getElementById("test").innerHTML = "You've pressed the key: " + a;   }

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

415 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

Advertisements