Web Design - 9
Web Design - 9
JavaScript Event 2
In html, there are various events which represents that some activity is performed by
اٌّحصفحٚا اٌّسحخذَ أَٙ بٛ ٕ٘ان اٌؼذٌذ ِٓ األحذاخ اٌحً جشٍز إٌى أْ بؼط األٔشطة ٌمHTML ًف
اٌذيٚ ، js دٌٛحُ أظافة و، HTML ق أي ػٕصز ِٓ ػٕاصزٛ ػٕذِا ٌٕمز اٌّسحخذَ ف، ػٍى سبًٍ اٌّثاي
mouseover onmouseover When the cursor of the mouse comes over the element
mousedown onmousedown When the mouse button is pressed over the element
mouseup onmouseup When the mouse button is released over the element
Form Events
Event Performed Event Handler Description
focus onfocus When the user focuses on an 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
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
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
As a Method
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 ٌؼًّ ٘ذا اٌحذخ ػٕذِا ٌسحمبً ػٕصز
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
.ٌٗ خارج أحذ اٌؼٕاصز اٌفزػٍةٚس خارج ػٕصز ِا أٚػٕذِا ٌححزن ِؤشز اٌّا 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
.ٌٗ ػٍى أحذ اٌؼٕاصز اٌفزػٍةٚق ػٕصز أٛس فٚ ػٕذِا ٌححزن ِؤشز اٌّا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
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
The prompt() method in JavaScript is used to display a prompt box that prompts the
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.
This method continues the calling of function until the window is closed or
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.
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
event.
ٍٓ ٌّىٕٕا إظافة اوثز ِٓ حذخ إٌى ػٕصز ِؼ.JavaScript ًً٘ داٌة ِعّٕة ف
.ة بزِجة اٌحطبٍماتٙاجٚ اسطةٛ إٔشاؤ٘ا بْٚ األحذاخ ِٓ إٔشاء اٌّسحخذَ أٌّٛىٓ أْ جى
addEventListener() Method
Parameter Values
event's name.
Suppose we have a div element and a paragraph element inside it, and we are applying
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
It means that in bubbling, the inner element's event is handled first, and then the
.div ثُ ٌحُ اٌحؼاًِ ِغ حذخ ػٕصز، h2 جحُ ِؼاٌجة حذخ اٌؼٕصز، Bubbling ًف
ثُ ٌحُ اٌحؼاًِ ِغ حذخ اٌؼٕصز، ل اٚ ٌحُ اٌحؼاًِ ِغ حذخ اٌؼٕصز اٌذاخًٍ أ، Bubbling ً٘ذا ٌؼًٕ أٔٗ ف
.ًاٌخارج
JavaScript addEventListener() Method 44
In Capturing the event of div element is handled first, and then the paragraph
It means that in capturing the outer element's event is handled first, and then the
h2 ثُ ٌحُ اٌحؼاًِ ِغ حذخ ػٕصز، ل اٚ ٌحُ اٌحؼاًِ ِؼٗ أdiv جحُ ِؼاٌجة حذخ ػٕصزCapturing ًف
ثُ ٌحُ اٌحؼاًِ ِغ حذخ اٌؼٕصز، ل اٚ ٌحُ اٌحؼاًِ ِغ حذخ اٌؼٕصز اٌخارجً أCapturing ٘ذا ٌؼًٕ أٔٗ ػٕذ
.ًٍاٌذاخ
JavaScript addEventListener() Method 45
When it is set to False (which is its default value), then the event uses Bubbling
propagation,