Found 10711 Articles for Web Development

How to suppress jQuery event handling temporarily?

Alex Onsman
Updated on 14-Feb-2020 12:34:23

515 Views

To suppress jQuery event handling temporarily, add a class to your element so that you can prevent further code from execution.ExampleYou can try to run the following code to learn how to suppress jQuery event handling −Live Demo $(document).ready(function(){     $(element).click(function(e) {         e.preventDefault();         // Check for fired class         if ($(this).hasClass('fired') == false) {           // Add fired class           $(element).addClass('fired');           // Remove fired class           $(element).removeClass('fired');           }    }); }); .fired {     font-size: 25px;     color: green; } This is a heading This is a paragraph. This is second paragraph.

How to override jQuery event handlers?

Alex Onsman
Updated on 14-Feb-2020 12:33:20

2K+ Views

Use the off() method to override jQuery event handlers. This method is used to remove an event handler. The on() method is used to attach one or more event handler.ExampleYou can try to run the following code to learn how to override jQuery event handlers −Live Demo           jQuery off() method                              $(document).ready(function() {             function aClick() {                $("div").show().fadeOut("slow");             }                             $("#bind").click(function () {                $("#theone").on("click", aClick).text("Can Click!");             });                             $("#unbind").click(function () {                $("#theone").off("click", aClick).text("Does nothing...");             });                          });                              button {              margin:5px;          }          button#theone {              color:red;              background:yellow;          }                             Does nothing...       Bind Click       Unbind Click               Click!                

How to implement custom events in jQuery?

Alex Onsman
Updated on 14-Feb-2020 12:32:17

870 Views

Custom event means you can create your own events in jQuery. For example, create a custom event to fire an alert box on pressing any key on the keyboard.ExampleYou can try to run the following code to learn how to create a custom event,Live Demo $(document).ready(function(){    $('textarea').bind("enterKey",function(e){      alert("You have pressed Enter key!");    });    $('textarea').keyup(function(e){      if(e.keyCode == 13)      {         $(this).trigger("enterKey");      }    }); });  

How to store and reproduce jQuery events?

Alex Onsman
Updated on 11-Dec-2019 07:10:43

133 Views

To store and reproduce jQuery events, use console to log.ExampleYou can try to run the following code to learn how to store and reproduce jQuery events:Live Demo $(document).ready(function(){     window.events = []     $("#track").click(function(event){        window.events.push(event)            $.each(window.events, function(i, item){           console.log(i, item);        });    }); }); Track

Which jQuery events do not bubble?

Alex Onsman
Updated on 11-Dec-2019 07:22:01

119 Views

Some of the jQuery events, do not bubble such as mouseenter do not bubble. Let us see an example of such events.ExampleYou can try to run the following code to learn how to work with jQuery events, which do not bubble,Live Demo  $(document).ready(function(){     $("p").mouseenter(function(){       $("p").css("background-color", "red");     });     $("p").mouseleave(function(){       $("p").css("background-color", "blue");     });  }); Demo Text - Keep the mouse pointer here.

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

Alex Onsman
Updated on 11-Dec-2019 07:24:07

153 Views

stopPropogation() methodTo stop the bubbling of an event to parent elements, use the stopPropagation() method.ExampleYou can try to run the following code to learn how to work with stopPropogation() method:Live Demo           jQuery stopPropagation() method                              $(document).ready(function() {             $("div").click(function(event){                alert("This is : " + $(this).text());                                event.stopPropagation();             ... Read More

How we can prioritize jQuery events?

Alex Onsman
Updated on 11-Dec-2019 07:25:43

763 Views

To prioritize jQuery events, use the event.stopPropagation().ExampleYou can try to run the following code to learn how to prioritize using stopPropogation() method:Live Demo $(document).ready(function(){        var timer;    function out(s) {       if (timer) {         clearTimeout(timer);         timer = null;       }       $("#demo").append(s + "");       timer = setTimeout(function() {         $("#demo").append("-------" + "");         timer = null;       }, 100);    }    $(".li").find('input').click(function(e){     out('li>input');     if ($(this).parent().hasClass("stop")) {         e.stopPropagation();     }    });    $(".li").click(function(e){     out('li');    });    $('input').click(function(e){     out('input');     if ($(this).parent().hasClass("stop")) {         e.stopPropagation();     }    }); }); Demo Demo using stop propagation method

How to fire jQuery events with setTimeout?

Alex Onsman
Updated on 11-Dec-2019 07:28:43

1K+ Views

The jQuery setTimeout() method is used to set an interval for events to fire.ExampleHere, we will set an interval of 3 seconds for an alert box to load using jQuery events:Live Demo $(document).ready(function(){     $("#button1").click(function(){        setTimeout("alert('Hello World!');", 3000);     }); }); Click Click the above button and wait for 3 seconds. An alert box will generate after 3 seconds.

Is it possible to detect when images are loaded via a jQuery event?

Alex Onsman
Updated on 11-Dec-2019 07:27:19

2K+ Views

To detect loading of an image with jQuery, use the load() event handler.Note: The load() method deprecated in jQuery version 1.8. It was completely removed in version 3.0. To see its working, add jQuery version for CDN before 3.0.ExampleYou can try to run the following code to learn how to detect when image loads:Live Demo $(document).ready(function(){     $("img").load(function(){         alert("Image successfully loaded.");     }); }); Note: The load() method deprecated in jQuery version 1.8. It was completely removed in version 3.0. To see its working, add jQuery version for CDN before 3.0.

Are jQuery events blocking?

Alex Onsman
Updated on 14-Feb-2020 12:28:52

167 Views

Ti check whether jQuery events are blocking, use the .triggerHandler() method, since it returns anything the last event handler for that event on that selector returns.ExampleLive Demo $(document).ready(function(){     var myValue = "John";     $("body").bind("eventName", function(e, value) {        return value + " Jacob";     });    var result = $("body").triggerHandler("eventName", myValue);    alert(result); }); This shows an alert box.

Advertisements