0% found this document useful (0 votes)
0 views47 pages

Web Design - 9

The document explains JavaScript events, which are changes in the state of an object triggered by user actions or browser activities. It details various types of events, including mouse, form, keyboard, and window events, along with their corresponding event handlers and descriptions. Additionally, it covers methods for applying event handlers, such as HTML attributes, methods attached to elements, and the addEventListener() method.

Uploaded by

wwwbby2040
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views47 pages

Web Design - 9

The document explains JavaScript events, which are changes in the state of an object triggered by user actions or browser activities. It details various types of events, including mouse, form, keyboard, and window events, along with their corresponding event handlers and descriptions. Additionally, it covers methods for applying event handlers, such as HTML attributes, methods attached to elements, and the addEventListener() method.

Uploaded by

wwwbby2040
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Web Design

JavaScript Event 2

 The change in the state of an object is known as an Event.

 In html, there are various events which represents that some activity is performed by

the user or by the browser. When javascript code is included in HTML

.‫ ) باسُ اٌحذخ‬HTML ‫ٌُؼزف اٌحغٍٍز فً حاٌة اٌىائٓ (ػٕاصز‬ 

‫ اٌّحصفح‬ٚ‫ا اٌّسحخذَ أ‬ٙ‫َ ب‬ٛ‫ ٕ٘ان اٌؼذٌذ ِٓ األحذاخ اٌحً جشٍز إٌى أْ بؼط األٔشطة ٌم‬HTML ً‫ف‬ 

HTML ً‫د جافا سىزٌبث ف‬ٛ‫ػٕذِا ٌحُ جعٍّٓ و‬

‫اٌذي‬ٚ ، js ‫د‬ٛ‫ٌحُ أظافة و‬، HTML ‫ق أي ػٕصز ِٓ ػٕاصز‬ٛ‫ ػٕذِا ٌٕمز اٌّسحخذَ ف‬، ‫ػٍى سبًٍ اٌّثاي‬ 

.‫ّة اٌحً سٍحُ جٕفٍذ٘ا فً اٌحذخ‬ٌّٙ‫سٍٕفذ ا‬


JavaScript Event >> Mouse Events 3

Event Performed Event Handler Description


click onclick When mouse click on an element

mouseover onmouseover When the cursor of the mouse comes over the element

mouseout onmouseout When the cursor of the mouse leaves an element

mousedown onmousedown When the mouse button is pressed over the element

mouseup onmouseup When the mouse button is released over the element

mousemove onmousemove When the mouse movement takes place.


JavaScript Event 4

Form Events
Event Performed Event Handler Description
focus onfocus When the user focuses on an element

submit onsubmit When the user submits the form

blur onblur When the focus is away from a form element

Change onchange When the user modifies or changes the value of a form
element

Keyboard Events
Keydown & Keyup onkeydown & When the user press and then release the key
onkeyup
JavaScript Event >> Window Events 5

Event Performed Event Handler Description


load onload When the browser finishes the loading of the page

unload onunload When the visitor leaves the current webpage, the
browser unloads it
resize onresize When the visitor resizes the window of the browse
JavaScript Event 6

 There are three common methods for applying event handlers to items within

 our pages:

 As an HTML attribute

 As a method attached to the Element

 Using addEventListener()
JavaScript Event 7

 As an HTML Attribute

 Attribute You can specify the function to be run in an attribute in the markup, as shown

in the following example

< body onclick= “ myFunction() ;“ >


/* myFunction will now run when the user clicks anything within 'body' */
JavaScript Event 8

 As a Method

 We can attach functions by using helpers already built into JavaScript:

window.onclick = myFunction ;
/* myFunction will now run when the user clicks
anything within browser window */

window.onclick = myfunction {
/* myFunction will now run when the user clicks
anything within browser window */ };
Example 1 >> JavaScript Event 9
Example 2 >> JavaScript Event 10

 oninput Event The oninput event occurs when an element gets user input.

 This event occurs when the value of an <input> or <textarea> element is changed.

ُ‫ ٌحذخ ٘ذا اٌحذخ ػٕذِا ٌح‬.َ‫ ِذخالت ِٓ لبً اٌّسحخذ‬HTML ‫ٌؼًّ ٘ذا اٌحذخ ػٕذِا ٌسحمبً ػٕصز‬ 

.>textarea< ٚ‫> أ‬input< ‫جغٍٍز لٍّة ػٕصز‬


Example 3 >> JavaScript Event 11
Example 4 >> JavaScript Event 12
Example 4 >> JavaScript Event 13
Example 5 >> JavaScript Event 14
Example 5 >> JavaScript Event 15
Example 5 >> JavaScript Event 16
JavaScript Event 17

 the onmouseleave Event occurs when the mouse pointer is moved out of an element.

.‫س خارج ػٕصز ِا‬ٚ‫ ػٕذ جحزٌه ِؤشز اٌّا‬onmouseleave ‫ٌحُ جٕفٍذ حذخ‬ 

 The onmouseout Event occurs when the mouse pointer is moved out of an element, or out

of one of its children.

.ٌٗ ‫ خارج أحذ اٌؼٕاصز اٌفزػٍة‬ٚ‫س خارج ػٕصز ِا أ‬ٚ‫ػٕذِا ٌححزن ِؤشز اٌّا‬ onmouseout ‫ٌحُ جٕفٍذ حذخ‬ 
JavaScript Event 18

 The onmousemove Event occurs when the pointer is moving while it is over an element.

.‫ق ػٕصز‬ٛ‫دٖ ف‬ٛ‫ج‬ٚ ‫ ػٕذِا ٌححزن اٌّؤشز أثٕاء‬onmousemove ‫ٌحُ جٕفٍذ حذخ‬ 

 The onmouseover Event occurs when the mouse pointer is moved onto an element, or

onto one of its children.

.ٌٗ ‫ ػٍى أحذ اٌؼٕاصز اٌفزػٍة‬ٚ‫ق ػٕصز أ‬ٛ‫س ف‬ٚ‫ ػٕذِا ٌححزن ِؤشز اٌّا‬onmouseover ‫ٌحُ جٕفٍذ حذخ‬ 

 The onmousedown Event occurs when a user presses a mouse button over an element.

.‫ق ػٕصز‬ٛ‫س ف‬ٚ‫ ػٕذِا ٌعغط اٌّسحخذَ ػٍى سر اٌّا‬onmousedown ‫ٌحُ جٕفٍذ حذخ‬ 
Example 6 >> JavaScript Event 19
Example 6 >> JavaScript Event 20
JavaScript Event >> charCode 21

 The charCode property returns the Unicode character code of the key that triggered

the onkeypress event.


Example 7 >> charCode 22
Example 7 >> charCode 23
Example 7 >> charCode 24
Example 8 >> JavaScript Event 25
JavaScript Hide Elements 26

 in JavaScript, we can hide the elements using the style.display or by using the

style.visibility.

 in HTML , we can use the hidden attribute to hide the specified element. When the

hidden attribute in HTML sets to true, the element is hidden, or when the value is

false, the element is visible.


JavaScript Hide Elements 27
JavaScript prompt() dialog box 28

 The prompt() method in JavaScript is used to display a prompt box that prompts the

user for the input.

 It is generally used to take the input from the user before entering the page.

 When the prompt box pops up, we have to click "OK" or "Cancel" to proceed.

 The box is displayed using the prompt() method, which takes two arguments:

 The first argument >> message: It is an optional parameter. It is the text displays to

the user.

 The second argument >>default: It is also an optional parameter. It is a string that

contains the default value displayed in the textbox.


Example 9 >> prompt() dialog box 29
Example 9 >> prompt() dialog box 30
JavaScript setInterval() Method 31

 The setInterval() method in JavaScript is used to repeat a specified function at every

given time-interval. It evaluates an expression or calls a function at given intervals.

 This method continues the calling of function until the window is closed or

the clearInterval() method is called.

 This method takes two parameter values function and milliseconds that are defined as

follows.
JavaScript setInterval() Method 32

 function: It is the function containing the block of code that will be executed.

 milliseconds: This parameter represents the length of the time interval between each

execution.

 How to stop the execution?

 We can use the clearInterval() method to stop the execution of the function specified

in setInterval() method. The value returned by the setInterval() method can be used as

the argument of clearInterval() method to cancel the timeout.


Example 10 >> setInterval() 33
Example 11 >> setInterval() 34
Example 11 >> setInterval() 35
Example 11 >> setInterval() 36
JavaScript addEventListener() Method 37

 the addEventListener() method is an inbuilt function of JavaScript. We can add multiple

event handlers to a particular element

 Events can be user-generated or generated by API's.

 An event listener is a JavaScript's procedure that waits for the occurrence of an

event.

ٍٓ‫ ٌّىٕٕا إظافة اوثز ِٓ حذخ إٌى ػٕصز ِؼ‬.JavaScript ً‫ً٘ داٌة ِعّٕة ف‬ 

.‫ة بزِجة اٌحطبٍمات‬ٙ‫اج‬ٚ ‫اسطة‬ٛ‫ إٔشاؤ٘ا ب‬ٚ‫ْ األحذاخ ِٓ إٔشاء اٌّسحخذَ أ‬ٛ‫ٌّىٓ أْ جى‬ 

. ‫ع اٌحذخ ِٓ احذ اٌؼٕاصز‬ٛ‫ل‬ٚ ‫ إجزاء ٌجافا سىزٌبث ٌٕحظز‬ٛ٘ ‫ِسحّغ اٌحذخ‬ 


JavaScript addEventListener() Method 38

 addEventListener() Method

 We can attach functions by using helpers already built into JavaScript:

window. addEventListener( "click", myFunction ) ;


/* myFunction will now run when the user clicks
anything within browser window */

window. addEventListener( "click", myfunction(e) {


/* myFunction will now run when the user clicks anything within
browser window */ } );
JavaScript addEventListener() Method 39

Parameter Values

 event: It is a required parameter. It can be defined as a string that specifies the

event's name.

 function: It is also a required parameter. It is a JavaScript function which responds to

the event occur.


Example 10 >> addEventListener() 40
Example 11 >> addEventListener() 41
JavaScript addEventListener() Method 42

 Suppose we have a div element and a paragraph element inside it, and we are applying

the "click" event to both of them using the addEventListener() method.

 Now the question is on clicking the paragraph element, which element's click event is

handled first.

‫" ػٍى والّ٘ا باسحخذاَ داٌة‬click" ‫ٔطبك حذخ‬ٚ ، h2 ً‫ػٕصز اخز بذاخٍٗ ِث‬ٚ div ‫ٌٕفحزض أْ ٌذٌٕا ػٕصز‬ 

.addEventListener()

.‫ل ا‬ٚ‫ أي ػٕصز ٌحُ اٌحؼاًِ ِغ حذخ اٌعغط أ‬، ‫ّا‬ٍٍٙ‫اٌسؤاي اَْ ػٕذ اٌعغط ػ‬ 
JavaScript addEventListener() Method 43

 So, in Bubbling, the event of paragraph element is handled first, and then the div

element's event is handled.

 It means that in bubbling, the inner element's event is handled first, and then the

outermost element's event will be handled.

.div ‫ ثُ ٌحُ اٌحؼاًِ ِغ حذخ ػٕصز‬، h2 ‫ جحُ ِؼاٌجة حذخ اٌؼٕصز‬، Bubbling ً‫ف‬ 

‫ ثُ ٌحُ اٌحؼاًِ ِغ حذخ اٌؼٕصز‬، ‫ل ا‬ٚ‫ ٌحُ اٌحؼاًِ ِغ حذخ اٌؼٕصز اٌذاخًٍ أ‬، Bubbling ً‫٘ذا ٌؼًٕ أٔٗ ف‬ 

.ً‫اٌخارج‬
JavaScript addEventListener() Method 44

 In Capturing the event of div element is handled first, and then the paragraph

element's event is handled.

 It means that in capturing the outer element's event is handled first, and then the

innermost element's event will be handled.

h2 ‫ ثُ ٌحُ اٌحؼاًِ ِغ حذخ ػٕصز‬، ‫ل ا‬ٚ‫ ٌحُ اٌحؼاًِ ِؼٗ أ‬div ‫ جحُ ِؼاٌجة حذخ ػٕصز‬Capturing ً‫ف‬ 

‫ ثُ ٌحُ اٌحؼاًِ ِغ حذخ اٌؼٕصز‬، ‫ل ا‬ٚ‫ ٌحُ اٌحؼاًِ ِغ حذخ اٌؼٕصز اٌخارجً أ‬Capturing ‫٘ذا ٌؼًٕ أٔٗ ػٕذ‬ 

.ًٍ‫اٌذاخ‬
JavaScript addEventListener() Method 45

addEventListener (event, function, useCapture ) ;

 We can specify the propagation using the useCapture parameter.

 When it is set to False (which is its default value), then the event uses Bubbling

propagation,

 And when it is set to True, there is the Capturing propagation..


Example 12 >> addEventListener() 46
Example 12 >> addEventListener() 47

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy