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
What Are Clauses in English
By internTk21 Thu 11 Apr 2024 10:05 pm Board English Language
0
37
Thu 11 Apr 2024 10:05 pm By internTk21 View Topic What Are Clauses in English
Getting to Know the Main Rules for Forming Plural Nouns
By internTk21 Thu 11 Apr 2024 3:47 pm Board English Language
0
33
Thu 11 Apr 2024 3:47 pm By internTk21 View Topic Getting to Know the Main Rules for Forming Plural Nouns
How to Change the Title of Each Page on Joomla
By internTk21 Thu 11 Apr 2024 2:15 pm Board English Language
0
18
Thu 11 Apr 2024 2:15 pm By internTk21 View Topic How to Change the Title of Each Page on Joomla
Recommendation of a Newsberg Template on Joomla: News Website
By internTk21 Thu 11 Apr 2024 1:22 pm Board English Language
0
19
Thu 11 Apr 2024 1:22 pm By internTk21 View Topic Recommendation of a Newsberg Template on Joomla: News Website
The Use of Menu Manager- Menu Management on Joomla 4.0
By internTk21 Thu 11 Apr 2024 11:05 am Board English Language
0
25
Thu 11 Apr 2024 11:05 am By internTk21 View Topic The Use of  Menu Manager- Menu Management on Joomla 4.0
How to Change a Template on a Joomla 4 to Be an Interesting Website
By internTk21 Thu 11 Apr 2024 9:50 am Board English Language
0
22
Thu 11 Apr 2024 9:50 am By internTk21 View Topic How to Change a Template on a Joomla 4 to Be an Interesting Website
Mastering Syllable Stress: Key to Natural English Pronunciation
By internTk21 Wed 10 Apr 2024 10:38 pm Board English Language
0
21
Wed 10 Apr 2024 10:38 pm By internTk21 View Topic Mastering Syllable Stress: Key to Natural English Pronunciation
Commonly Used Phrases in Argumentative Essays
By internTk21 Wed 10 Apr 2024 10:16 pm Board English Language
0
14
Wed 10 Apr 2024 10:16 pm By internTk21 View Topic Commonly Used Phrases in Argumentative Essays
The Role of Punctuation Marks in English Writing
By internTk21 Wed 10 Apr 2024 10:00 pm Board English Language
0
11
Wed 10 Apr 2024 10:00 pm By internTk21 View Topic The Role of Punctuation Marks in English Writing
English Phonetics: Understanding Voiced and Voiceless Sounds
By internTk21 Wed 10 Apr 2024 9:41 pm Board English Language
0
13
Wed 10 Apr 2024 9:41 pm By internTk21 View Topic English Phonetics: Understanding Voiced and Voiceless Sounds
Expanding Vocabulary: Learning Antonyms in English
By internTk21 Wed 10 Apr 2024 9:10 pm Board English Language
0
11
Wed 10 Apr 2024 9:10 pm By internTk21 View Topic Expanding Vocabulary: Learning Antonyms in English
Mastering Noun Modifiers in English
By internTk21 Wed 10 Apr 2024 8:53 pm Board English Language
0
25
Wed 10 Apr 2024 8:53 pm By internTk21 View Topic Mastering Noun Modifiers in English
How to Change Language into Thai on Joomla 4.0 for More User-friendly
By internTk21 Wed 10 Apr 2024 4:21 pm Board English Language
0
17
Wed 10 Apr 2024 4:21 pm By internTk21 View Topic How to Change Language into Thai on Joomla 4.0 for More User-friendly
How to Update Joomla Version 4 by Joomla Package File (Manual Update)
By internTk21 Wed 10 Apr 2024 3:29 pm Board English Language
0
17
Wed 10 Apr 2024 3:29 pm By internTk21 View Topic How to Update Joomla Version 4 by Joomla Package File (Manual Update)
How to Create Bilingual Website on Joomla 4
By internTk21 Wed 10 Apr 2024 10:50 am Board English Language
1
30
Wed 10 Apr 2024 11:00 am By internTk21 View Topic How to Create Bilingual Website on Joomla 4
Pros and Cons of Joomla: Website Management CMS Program
By internTk21 Wed 10 Apr 2024 10:34 am Board English Language
0
134
Wed 10 Apr 2024 10:34 am By internTk21 View Topic Pros and Cons of Joomla: Website Management CMS Program
How to Delete an Article on Joomla 4.0
By internTk21 Tue 09 Apr 2024 3:18 pm Board English Language
0
39
Tue 09 Apr 2024 3:18 pm By internTk21 View Topic How to Delete an Article on Joomla 4.0
Phrasal Verbs in Daily Life
By internTk21 Tue 09 Apr 2024 2:10 pm Board English Language
0
31
Tue 09 Apr 2024 2:10 pm By internTk21 View Topic Phrasal Verbs  in Daily Life
Intonation in English
By internTk21 Tue 09 Apr 2024 1:41 pm Board English Language
0
51
Tue 09 Apr 2024 1:41 pm By internTk21 View Topic Intonation in English
What is Joomla? It is a program that helps make a website, Content Management System (CMS) that is popular and modern.
By internTk21 Fri 05 Apr 2024 4:36 pm Board English Language
0
71
Fri 05 Apr 2024 4:36 pm By internTk21 View Topic What is Joomla?  It is a program that helps make a website, Content Management System (CMS) that is popular and modern.