JavaScript Events
HTML events are “things” that happen to HTML elements.
When JavaScript is used in HTML pages, JavaScript can “react” on these events.
HTML Events
An HTML event can be something the browser does, or something a user does.
Here are some examples of HTML events:
- An HTML web page has finished loading
- An HTML input field was changed
- An HTML button was clicked
Often, when events happen, you may want to do something.
JavaScript lets you execute code when events are detected.
HTML allows event handler attributes, with JavaScript code, to be added to HTML elements.
With single quotes:
<element event='some JavaScript'>
With double quotes:
<element event="some JavaScript">
In the following example, an onclick
attribute (with code), is added to a <button>
element:
Example
<button onclick="document.getElementById('demo').innerHTML = Date()">The time is?</button>
In the example above, the JavaScript code changes the content of the element with id=”demo”.
In the next example, the code changes the content of its own element (using this.innerHTML
):
Example
<button onclick="this.innerHTML = Date()">The time is?</button>
JavaScript code is often several lines long. It is more common to see event attributes calling functions:
Example
<button onclick="displayDate()">The time is?</button>
Common HTML Events
Here is a list of some common HTML events:
Event | Description |
---|---|
onchange | An HTML element has been changed |
onclick | The user clicks an HTML element |
onmouseover | The user moves the mouse over an HTML element |
onmouseout | The user moves the mouse away from an HTML element |
onkeydown | The user pushes a keyboard key |
onload | The browser has finished loading the page |
JavaScript Event Handlers
Event handlers can be used to handle and verify user input, user actions, and browser actions:
- Things that should be done every time a page loads
- Things that should be done when the page is closed
- Action that should be performed when a user clicks a button
- Content that should be verified when a user inputs data
- And more …
Many different methods can be used to let JavaScript work with events:
- HTML event attributes can execute JavaScript code directly
- HTML event attributes can call JavaScript functions
- You can assign your own event handler functions to HTML elements
- You can prevent events from being sent or being handled
- And more …
41 Responses
buy levofloxacin online order levaquin pill
avodart online order dutasteride for sale online ondansetron 8mg pill
buy spironolactone 25mg online cheap purchase spironolactone generic buy fluconazole pill
ampicillin cost cheap bactrim 960mg purchase erythromycin without prescription
order sildenafil careprost over the counter robaxin cost
cheap suhagra buy sildenafil pill estrace 2mg pills
order lamotrigine without prescription order vermox generic cost tretinoin
cost tadalafil 20mg tadalis 10mg usa voltaren 50mg for sale
buy isotretinoin 20mg generic generic isotretinoin 40mg order generic zithromax 500mg
indomethacin over the counter purchase indocin amoxicillin 500mg usa
tadalafil tablets cheap tadalafil 20mg canadian viagra
anastrozole 1mg price order anastrozole without prescription viagra cost
tadalafil 10mg prix tadalafil 10mg sans ordonnance sildenafil 50mg en france
deltasone 40mg drug viagra 100mg pills viagra mail order us
cialis 10mg für männer viagra 100mg kaufen ohne rezept sildenafil 200mg generika rezeptfrei kaufen
accutane tablet stromectol 12mg tablet buy stromectol canada
oral doxycycline furosemide ca lasix medication
ramipril 10mg for sale buy azelastine 10ml for sale buy astelin 10 ml sprayer
buy generic catapres cheap clonidine 0.1 mg order tiotropium bromide generic
buy buspirone 10mg online amiodarone 100mg pill purchase ditropan without prescription
buy terazosin 1mg generic order sulfasalazine generic buy azulfidine online
alendronate us buy panadol sale pepcid 20mg drug
order olmesartan 20mg for sale order olmesartan 20mg oral acetazolamide 250 mg
tacrolimus buy online prograf 5mg oral urso medication
imdur brand cheap imuran order micardis 20mg without prescription
buy zyban 150 mg generic cost zyrtec 5mg seroquel 100mg pill
cost molnupiravir 200mg prevacid price purchase lansoprazole generic
sertraline 50mg brand order lexapro 10mg purchase sildenafil for sale
order imuran pills oral salbutamol 100 mcg order sildenafil 100mg generic
tadalafil 20mg oral safe generic viagra sites sildenafil us
tadalafil 10mg tablet phenazopyridine 200mg brand symmetrel 100 mg oral
generic naltrexone order generic femara 2.5 mg order abilify 20mg pill
dapsone oral avlosulfon 100 mg uk buy aceon 4mg generic
buy medroxyprogesterone 10mg buy praziquantel 600mg generic cyproheptadine 4mg generic
modafinil usa order provigil 200mg online ivermectin usa
luvox ca cheap luvox 100mg glucotrol 10mg uk
order isotretinoin 20mg pill order prednisone 10mg order prednisone 40mg online
piracetam tablet order nootropil 800mg viagra 25 mg
azithromycin brand order zithromax 500mg generic gabapentin 800mg generic
furosemide drug purchase doxycycline sale plaquenil 200mg usa
order generic cialis 20mg cialis 5mg pill order anafranil 25mg online cheap