JavaScript Events

JavaScript Events

JavaScript events are actions or occurrences that happen in the browser. JavaScript can respond to these events and make the webpage interactive.

Examples of events:

  • Clicking a button

  • Typing in a text box

  • Hovering the mouse

  • Loading a web page

  • Submitting a form


πŸ“Œ Common Browser Events

Event Description
onclick User clicks an element
onmouseover Mouse moves over an element
onmouseout Mouse leaves an element
onchange Content of input field changes
onkeydown A key is pressed
onload Page or image finishes loading

Example 1: Click Event


 


Β Example 2: Mouse Over Event


 


Β Example 3: addEventListener() (Best Practice)


 


Example 4: Input Field Change Event


 


🎯 Why Use Events?

Events allow JavaScript to:

βœ” Create interactive pages
βœ” Validate forms
βœ” React to user behavior
βœ” Build dynamic UI features


πŸ“Œ Using addEventListener() vs Inline Events

Feature Inline Event addEventListener
Cleaner code ❌ No βœ” Yes
Multiple handlers possible ❌ No βœ” Yes
Modern approach ❌ Old βœ” Recommended

Example:


πŸš€ Summary

  • Events trigger actions in JavaScript.

  • You can handle events using:
    βœ” Inline event attributes (onclick, onchange, etc.)
    βœ” addEventListener() (recommended)

CodeCapsule

Sanjit Sinha β€” Web Developer | PHP β€’ Laravel β€’ CodeIgniter β€’ MySQL β€’ Bootstrap Founder, CodeCapsule β€” Student projects & practical coding guides. Email: info@codecapsule.in β€’ Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *