انتقل للمحتوى
Robo EgyptRobo Egypt
☰ Course contents
Robo Web: home
0. Lab tour1
1. HTML: the page's content7
2. CSS: the page's look6
3. JavaScript: the actions7
4. The project: the to-do list3
5. Pro CSS: shadows and movement5
6. Arrays and building the page with code3
7. Events and forms3
8. Objects and time3
9. Canvas: drawing and games3
10. The final project: the memory game3

The button talks

Section 3: JavaScript: the actionsLesson 3.1 · ⏱ 8 min

document.getElementById("hello") gets the element by its id. onclick sets what happens when it is clicked. And textContent is the words written on the element.

HTML
<button id="hello">Click me</button>
CSS
body { font-family: Tahoma; text-align: center; padding: 30px; }
button { font-size: 22px; padding: 10px 24px; }
JavaScript
const btn = document.getElementById("hello");

btn.onclick = function () {
  btn.textContent = "Welcome! 🎉";
};
🖥️ The page in Robo Web:The page in Robo Web

📌 Another example: two buttons and two messages

Each button has an onclick that changes the same paragraph.

🌐 Two buttons and two messages
HTML
<p id="msg">Click a button</p>
<button id="hi">Hello</button>
<button id="bye">Bye</button>
CSS
body { font-family: Tahoma, sans-serif; }
button { font-size: 20px; padding: 8px 20px; }
#msg { font-size: 26px; }
JavaScript
document.getElementById("hi").onclick = function () {
  document.getElementById("msg").textContent = "Welcome 👋";
};
document.getElementById("bye").onclick = function () {
  document.getElementById("msg").textContent = "Goodbye 👋";
};
🖥️ The page in Robo Web:The page in Robo Web
✍️ Practise
The button talks.
✍️ Practise here in the lessonor open the lab in a new page ↗

🎯 Challenge yourself

Easy

A button that writes “Done!” in the paragraph.

👀 Show the solution
🌐 Solution
HTML
<p id="msg"></p>
<button id="go">Click</button>
CSS
body { font-family: Tahoma, sans-serif; }
button { font-size: 20px; padding: 8px 20px; }
JavaScript
document.getElementById("go").onclick = function () {
  document.getElementById("msg").textContent = "Done!";
};
🖥️ The page in Robo Web:The page in Robo Web

Result: Done!

Medium

A button that changes its own words to “Thank you”.

👀 Show the solution
🌐 Solution
HTML
<button id="go">Click me</button>
CSS
body { font-family: Tahoma, sans-serif; }
button { font-size: 20px; padding: 8px 20px; }
JavaScript
const b = document.getElementById("go");
b.onclick = function () {
  b.textContent = "Thank you 😊";
};
🖥️ The page in Robo Web:The page in Robo Web

Result: the button became “Thank you 😊”.

Hard

A button that hides the picture, and a button that shows it.

👀 Show the solution
🌐 Solution
HTML
<div id="pic" style="font-size:60px">🐱</div>
<button id="hide">Hide</button>
<button id="show">Show</button>
CSS
body { font-family: Tahoma, sans-serif; }
button { font-size: 20px; padding: 8px 20px; }
JavaScript
const pic = document.getElementById("pic");
document.getElementById("hide").onclick = function () { pic.style.display = "none"; };
document.getElementById("show").onclick = function () { pic.style.display = "block"; };
🖥️ The page in Robo Web:The page in Robo Web

Result: the cat disappeared.

Finished the lesson? Mark it so your progress counts.