What are Events?

All the different visitor's actions that a web page can respond to are called events.

An event represents the precise moment when something happens.

Examples:

  • moving a mouse over an element
  • selecting a radio button
  • clicking on an element

The term "fires" is often used with events. Example: "The keypress event fires the moment you press a key".

Here are some common DOM events:

Mouse Event Keyboard Event Form Event Document/Windows Event
click keypress submit load
dblclick keyup change resize
mouseenter keydown focus scroll
mouseleve   blur unload

 

jQuery Syntax For Event Methods

In jQuery, most DOM events have an equivalent jQuery method.

To assign a click event to all paragraphs on a page, you can do this:

$("p").click();

The next step is to define what should happen when the event fires. You must pass a function to the event:

$("p").click(function(){
// action goes here!!
});

Commonly Used jQuery Event Methods

$(document).ready()

The $(document).ready() method allows us to execute a function when the document is fully loaded. This event is already explained in the jQuery Syntax chapter.

click()

The click() method attaches an event handler function to an HTML element.

The function is executed when the user clicks on the HTML element.

The following example says: When a click event fires on a <p> element; hide the current <p> element:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("p").click(function(){
    $(this).hide();
  });
});
</script>
</head>
<body>

<p>If you click on me, I will disappear.</p>
<p>Click me away!</p>
<p>Click me too!</p>

</body>
</html>

dblclick()

The dblclick() method attaches an event handler function to an HTML element.

The function is executed when the user double-clicks on the HTML element:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("p").dblclick(function(){
    $(this).hide();
  });
});
</script>
</head>
<body>
<p>If you double-click on me, I will disappear.</p>
<p>Click me away!</p>
<p>Click me too!</p>
</body>
</html>

mouseenter()

The mouseenter() method attaches an event handler function to an HTML element.

The function is executed when the mouse pointer enters the HTML element:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("#p1").mouseenter(function(){
    alert("You entered p1!");
  });
});
</script>
</head>
<body>
<p id="p1">Enter this paragraph.</p>
</body>
</html>

mouseleave()

The mouseleave() method attaches an event handler function to an HTML element.

The function is executed when the mouse pointer leaves the HTML element:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("#p1").mouseleave(function(){
    alert("Bye! You now leave p1!");
  });
});
</script>
</head>
<body>
<p id="p1">This is a paragraph.</p>
</body>
</html>

mousedown()

The mousedown() method attaches an event handler function to an HTML element.

The function is executed, when the left mouse button is pressed down, while the mouse is over the HTML element:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("#p1").mousedown(function(){
    alert("Mouse down over p1!");
  });
});
</script>
</head>
<body>
<p id="p1">This is a paragraph.</p>
</body>
</html>

mouseup()

The mouseup() method attaches an event handler function to an HTML element.

The function is executed, when the left mouse button is released, while the mouse is over the HTML element:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("#p1").mouseup(function(){
    alert("Mouse up over p1!");
  });
});
</script>
</head>
<body>
<p id="p1">This is a paragraph.</p>
</body>
</html>

hover()

The hover() method takes two functions and is a combination of the mouseenter() and mouseleave() methods.

The first function is executed when the mouse enters the HTML element, and the second function is executed when the mouse leaves the HTML element:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("#p1").hover(function(){
    alert("You entered p1!");
    },
    function(){
    alert("Bye! You now leave p1!");
  }); 
});
</script>
</head>
<body>
<p id="p1">This is a paragraph.</p>
</body>
</html>

focus()

The focus() method attaches an event handler function to an HTML form field.

The function is executed when the form field gets focus:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("input").focus(function(){
    $(this).css("background-color","#cccccc");
  });
  $("input").blur(function(){
    $(this).css("background-color","#ffffff");
  });
});
</script>
</head>
<body>
Name: <input type="text" name="fullname"><br>
Email: <input type="text" name="email">
</body>
</html>

blur()

The blur() method attaches an event handler function to an HTML form field.

The function is executed when the form field loses focus:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("input").focus(function(){
    $(this).css("background-color","#cccccc");
  });
  $("input").blur(function(){
    $(this).css("background-color","#ffffff");
  });
});
</script>
</head>
<body>
Name: <input type="text" name="fullname"><br>
Email: <input type="text" name="email">
</body>
</html>

 

Recent Topics
Subject
Post Reply
Open
Recent
Printer Troubleshoot Tips
By tommathur Fri 08 Jul 2022 7:01 pm Board English Language
0
1721
Fri 08 Jul 2022 7:01 pm By tommathur View Topic Printer Troubleshoot Tips
介绍 MooZiiCart 的 Plugin Content Product Match , 链接内容、 购物卡、 晋升。
By alfie8522 Fri 08 Jul 2022 10:41 am Board Chinese Language - 简体中文
0
1474
Fri 08 Jul 2022 10:41 am By alfie8522 View Topic 介绍 MooZiiCart 的 Plugin Content Product Match , 链接内容、 购物卡、 晋升。
介绍 MZC Shipping Method 展示如何运输。
By alfie8522 Thu 07 Jul 2022 4:03 pm Board Chinese Language - 简体中文
0
878
Thu 07 Jul 2022 4:03 pm By alfie8522 View Topic 介绍 MZC Shipping Method 展示如何运输。
介绍 MZC 模块 Top Hits 显示浏览次数最多的产品。
By alfie8522 Wed 06 Jul 2022 12:12 pm Board Chinese Language - 简体中文
0
844
Wed 06 Jul 2022 12:12 pm By alfie8522 View Topic 介绍 MZC 模块 Top Hits 显示浏览次数最多的产品。
介绍 MZC 模块 Product Labels 对于显示产品标签,让产品与众不同的。
By alfie8522 Mon 04 Jul 2022 2:36 pm Board Chinese Language - 简体中文
0
837
Mon 04 Jul 2022 2:36 pm By alfie8522 View Topic 介绍 MZC 模块 Product Labels 对于显示产品标签,让产品与众不同的。
介绍 MZC 模块 Cart 对于在购物车中显示产品 ,当客户选择产品到购物车时。
By alfie8522 Sat 02 Jul 2022 10:57 am Board Chinese Language - 简体中文
0
913
Sat 02 Jul 2022 10:57 am By alfie8522 View Topic 介绍 MZC 模块 Cart 对于在购物车中显示产品 ,当客户选择产品到购物车时。
介绍 MZC Products Reviews 供客户留下评论、评论、评价产品。
By alfie8522 Fri 01 Jul 2022 1:12 pm Board Chinese Language - 简体中文
0
878
Fri 01 Jul 2022 1:12 pm By alfie8522 View Topic 介绍 MZC Products Reviews 供客户留下评论、评论、评价产品。
介绍 MZC Wishlist 对于展示客户感兴趣的产品
By alfie8522 Thu 30 Jun 2022 3:23 pm Board Chinese Language - 简体中文
0
833
Thu 30 Jun 2022 3:23 pm By alfie8522 View Topic 介绍 MZC Wishlist 对于展示客户感兴趣的产品
介绍 MZC Recent comment 对于评论每个产品的最新。
By alfie8522 Wed 29 Jun 2022 11:08 am Board Chinese Language - 简体中文
0
915
Wed 29 Jun 2022 11:08 am By alfie8522 View Topic 介绍 MZC Recent comment 对于评论每个产品的最新。
介绍 MZC Bestseller ,对于展示商店最畅销的产品。
By alfie8522 Mon 27 Jun 2022 12:50 pm Board Chinese Language - 简体中文
0
798
Mon 27 Jun 2022 12:50 pm By alfie8522 View Topic 介绍 MZC Bestseller ,对于展示商店最畅销的产品。
介绍 MZC 模块 Latest Products 对于展示店铺的新产品。
By alfie8522 Sat 25 Jun 2022 12:38 pm Board Chinese Language - 简体中文
0
813
Sat 25 Jun 2022 12:38 pm By alfie8522 View Topic 介绍 MZC 模块 Latest Products  对于展示店铺的新产品。
介绍 MZC 模块 MooZiiCart Currency ,用于多种货币显示产品价格。
By alfie8522 Fri 24 Jun 2022 11:32 am Board Chinese Language - 简体中文
0
771
Fri 24 Jun 2022 11:32 am By alfie8522 View Topic 介绍 MZC 模块 MooZiiCart Currency ,用于多种货币显示产品价格。
介绍 MZC 模块 MooZiiCart Category 用于显示产品类别 是网站的菜单。
By alfie8522 Thu 23 Jun 2022 11:32 am Board Chinese Language - 简体中文
0
722
Thu 23 Jun 2022 11:32 am By alfie8522 View Topic 介绍 MZC 模块 MooZiiCart Category 用于显示产品类别 是网站的菜单。
介绍 MZC 模块登录(Login) 对于自己的订单菜单登录商店。
By alfie8522 Wed 22 Jun 2022 6:48 pm Board Chinese Language - 简体中文
0
762
Wed 22 Jun 2022 6:48 pm By alfie8522 View Topic 介绍 MZC 模块登录(Login) 对于自己的订单菜单登录商店。
介绍 MZC 模块 Manufacturer 对于在网页上显示产品制造商。
By alfie8522 Tue 21 Jun 2022 1:53 pm Board Chinese Language - 简体中文
0
760
Tue 21 Jun 2022 1:53 pm By alfie8522 View Topic 介绍 MZC 模块 Manufacturer 对于在网页上显示产品制造商。
介绍 System MooZiiCart Auto Close 对于设置在线订购系统的开和关闭时间。
By alfie8522 Sat 11 Jun 2022 3:30 pm Board Chinese Language - 简体中文
0
919
Sat 11 Jun 2022 3:30 pm By alfie8522 View Topic 介绍 System MooZiiCart Auto Close 对于设置在线订购系统的开和关闭时间。
介绍 MDImporter Component 对于从文件导入数据 Excel 。
By alfie8522 Fri 10 Jun 2022 11:28 am Board Chinese Language - 简体中文
0
809
Fri 10 Jun 2022 11:28 am By alfie8522 View Topic 介绍 MDImporter Component 对于从文件导入数据 Excel 。
介绍 模块 MZC Slider ii 对于展示各种产品和在网页上制作幻灯片。
By alfie8522 Thu 09 Jun 2022 2:26 pm Board Chinese Language - 简体中文
0
843
Thu 09 Jun 2022 2:26 pm By alfie8522 View Topic 介绍 模块 MZC Slider ii 对于展示各种产品和在网页上制作幻灯片。
developing a new Joomla 4 version of an existing J3 site
By ishasharma Thu 09 Jun 2022 11:41 am Board English Language
0
648
Thu 09 Jun 2022 11:41 am By ishasharma View Topic developing a new Joomla 4 version of an existing J3 site
The Best Way to Learn English Fast: 7 Tips for Learning English
By manisharajput Wed 08 Jun 2022 9:20 pm Board English Language
1
1265
Mon 10 Oct 2022 5:02 am By kelly2121 View Topic The Best Way to Learn English Fast: 7 Tips for Learning English