On this page12 sections
Lecture 030: Global & Event Attributes
| Category | 5: Code, Characters & Standards |
| Lecture | 030 of 048 |
| Estimated video length | 12 min |
| Target | HTML5 · any modern browser |
| Level | Beginner |
Most attributes belong to particular elements:
srcto images,hrefto links. A few work on every element. These are the global attributes. A family ofon…attributes, the event attributes, run JavaScript when something happens.
Learning Objectives
- Use the main global attributes:
id,class,style,title,lang,dir,hidden,tabindex,contenteditable,draggable,spellcheck - Store your own data on elements with
data-* - React to events with
onclick,oninput,onchange,onsubmit,onmouseover,onkeydownandonload
1. The Global Attributes You Already Know
| Attribute | Purpose |
|---|---|
id | A unique name (Lecture 017) |
class | A shared name for styling (Lecture 017) |
style | Inline CSS (Lecture 006) |
title | A tooltip on hover (Lecture 004) |
2. Language and Direction: lang and dir
Example
<p lang="en">Welcome to our shop.</p>
<p lang="ur" dir="rtl">ہماری دکان میں خوش آمدید۔</p>dir="rtl" makes text flow right to left, for Urdu and Arabic.
3. hidden
Example
<p>You can see me.</p>
<p hidden>You can't see me, but I'm still in the HTML.</p>4. contenteditable and spellcheck
Example: click the box and type:
<div contenteditable="true" spellcheck="true" style="border: 1px dashed #868e96; padding: 10px;">
Click here and edit this text. Try a typo like "recieve".
</div>5. tabindex
tabindex controls whether an element can be reached with the Tab key.
| Value | Effect |
|---|---|
0 | Joins the normal Tab order |
-1 | Can be focused by JavaScript, but Tab skips it |
| A positive number | Forces an order. Avoid it; it confuses keyboard users |
Lecture 031 covers keyboard access in more depth.
6. data-*: Your Own Data
Any attribute starting with data- is yours to invent. JavaScript reads it through .dataset.
Example
<button type="button" data-price="450" data-name="Chai" onclick="document.getElementById('out').textContent = this.dataset.name + ' costs Rs ' + this.dataset.price">Chai</button>
<button type="button" data-price="600" data-name="Coffee" onclick="document.getElementById('out').textContent = this.dataset.name + ' costs Rs ' + this.dataset.price">Coffee</button>
<p id="out">Pick a drink.</p>7. Event Attributes
Example: try each one:
<button type="button" onclick="this.textContent = 'Clicked!'">onclick</button>
<p onmouseover="this.style.background = '#fff3bf'" onmouseout="this.style.background = ''">onmouseover: hover over me</p>
<input placeholder="oninput: type here" oninput="document.getElementById('echo').textContent = this.value">
<span id="echo"></span>
<p>
<select onchange="document.getElementById('pick').textContent = 'You chose ' + this.value">
<option>Tea</option><option>Coffee</option><option>Juice</option>
</select>
<span id="pick"></span>
</p>
<input placeholder="onkeydown: press a key" onkeydown="document.getElementById('key').textContent = 'Key: ' + event.key">
<span id="key"></span>| Group | Common events |
|---|---|
| Mouse | onclick, ondblclick, onmouseover, onmouseout |
| Keyboard | onkeydown, onkeyup |
| Form | oninput, onchange, onsubmit, onfocus, onblur |
| Window | onload, onresize, onscroll |
| Media | onplay, onpause, onended |
For larger projects, addEventListener in a script (Lecture 020) keeps JavaScript out of the HTML.
Common Beginner Mistakes
1. Hiding important content with hidden. It's invisible to everyone, including screen readers.
2. Positive tabindex values. They break the natural Tab order.
3. Invented attributes without data-. price="450" is invalid; data-price="450" is correct.
4. Huge inline event handlers. Move anything longer than a line into a script.
Practice Questions
- What makes an attribute "global"?
- Which attribute makes Urdu text flow right to left?
- Make a paragraph that the visitor can edit.
- Store a product id of 42 on a
<div>using a data attribute. - Which event fires every time the text in an input changes?
- What does
tabindex="-1"do?
Practice Answers
- It can be used on any HTML element.
dir="rtl"<p contenteditable="true">Edit me</p><div data-product-id="42">…</div>oninput- The element can be focused by JavaScript, but the Tab key skips it.
Key Takeaways
- Global attributes work everywhere:
id,class,style,title,lang,dir,hidden,tabindex,contenteditable… data-*attributes hold your own data; read them with.dataset.- Event attributes (
onclick,oninput,onchange…) run JavaScript when something happens.