On this page12 sections
  1. Learning Objectives
  2. 1. The Global Attributes You Already Know
  3. 2. Language and Direction: lang and dir
  4. 3. hidden
  5. 4. contenteditable and spellcheck
  6. 5. tabindex
  7. 6. data-*: Your Own Data
  8. 7. Event Attributes
  9. Common Beginner Mistakes
  10. Practice Questions
  11. Practice Answers
  12. Key Takeaways

Lecture 030: Global & Event Attributes

Category5: Code, Characters & Standards
Lecture030 of 048
Estimated video length12 min
TargetHTML5 · any modern browser
LevelBeginner

Most attributes belong to particular elements: src to images, href to links. A few work on every element. These are the global attributes. A family of on… 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, onkeydown and onload

1. The Global Attributes You Already Know

AttributePurpose
idA unique name (Lecture 017)
classA shared name for styling (Lecture 017)
styleInline CSS (Lecture 006)
titleA tooltip on hover (Lecture 004)

2. Language and Direction: lang and dir

Example

html
<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

html
<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:

html
<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.

ValueEffect
0Joins the normal Tab order
-1Can be focused by JavaScript, but Tab skips it
A positive numberForces 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

html
<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:

html
<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>
GroupCommon events
Mouseonclick, ondblclick, onmouseover, onmouseout
Keyboardonkeydown, onkeyup
Formoninput, onchange, onsubmit, onfocus, onblur
Windowonload, onresize, onscroll
Mediaonplay, 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

  1. What makes an attribute "global"?
  2. Which attribute makes Urdu text flow right to left?
  3. Make a paragraph that the visitor can edit.
  4. Store a product id of 42 on a <div> using a data attribute.
  5. Which event fires every time the text in an input changes?
  6. What does tabindex="-1" do?

Practice Answers

  1. It can be used on any HTML element.
  2. dir="rtl"
  3. <p contenteditable="true">Edit me</p>
  4. <div data-product-id="42">…</div>
  5. oninput
  6. 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.