×

javascript · level 2 · unit 3 · lesson 4

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

Attributes are an element's settings, and code can change them.

An is extra information inside an opening tag, like id and class. A link's href says where it goes. getAttribute("href") reads it, and gives it a new value.

Some attributes are on-or-off switches, with matching true or false properties. hides an element and false brings it back. switches a button off, so clicks do nothing.

Use them to show a message only when it is needed, or to stop someone pressing Buy twice.

<a id="shop" href="https://example.com/old">Visit the shop</a>
<p id="sale" hidden>Sale now on!</p>
<button id="buy">Buy</button>
const link = document.querySelector("#shop");
link.setAttribute("href", "https://example.com/new");
console.log(link.getAttribute("href"));
document.querySelector("#sale").hidden = false;
document.querySelector("#buy").disabled = true;

Press run it to bring the page to life.

console

nothing logged yet

In HTML, hidden and disabled need no value. Being there switches them on.