انتقل للمحتوى
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

Change the colour and read what is typed

Section 3: JavaScript: the actionsLesson 3.5 · ⏱ 8 min
🌐 A button that changes the colour
HTML
<div id="box"></div>
<button id="go">Change colour</button>
CSS
body { font-family: Tahoma; text-align: center; }
#box { width: 160px; height: 160px; margin: 20px auto; background: orange; border-radius: 20px; }
JavaScript
const box = document.getElementById("box");

document.getElementById("go").onclick = function () {
  box.style.background = "#5B3DF5";
};
🖥️ The page in Robo Web:The page in Robo Web
🌐 Read the visitor's name
HTML
<input id="name" placeholder="Your name">
<button id="go">go</button>
<h2 id="out"></h2>
CSS
body { font-family: Tahoma; text-align: center; padding: 20px; }
input, button { font-size: 20px; padding: 6px 12px; }
JavaScript
document.getElementById("go").onclick = function () {
  const name = document.getElementById("name").value;
  document.getElementById("out").textContent = "Hello, " + name + "!";
};
🖥️ The page in Robo Web:The page in Robo Web

📌 Another example: colours from a text box

We type the name of a colour in English, and the background turns that colour.

🌐 Colours from a text box
HTML
<input id="c" placeholder="red, gold, pink...">
<button id="go">Colour it</button>
CSS
body { font-family: Tahoma, sans-serif; }
button { font-size: 20px; padding: 8px 20px; }
input { font-size: 20px; }
JavaScript
document.getElementById("go").onclick = function () {
  const color = document.getElementById("c").value;
  document.body.style.background = color;
};
🖥️ The page in Robo Web:The page in Robo Web
✍️ Practise
A button that changes the colour.
✍️ Practise here in the lessonor open the lab in a new page ↗
✍️ Practise
Read what the visitor types.
✍️ Practise here in the lessonor open the lab in a new page ↗

🎯 Challenge yourself

Easy

A button that makes the background pink.

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

Result: a pink background.

Medium

A button that makes the words 4 pixels bigger with every click.

👀 Show the solution
🌐 Solution
HTML
<p id="t">Make me bigger</p>
<button id="big">A+</button>
CSS
body { font-family: Tahoma, sans-serif; }
button { font-size: 20px; padding: 8px 20px; }
#t { font-size: 16px; }
JavaScript
let size = 16;
document.getElementById("big").onclick = function () {
  size = size + 4;
  document.getElementById("t").style.fontSize = size + "px";
};
🖥️ The page in Robo Web:The page in Robo Web

Result: 3 clicks: 28px.

Hard

A text box for the name and another for the age, and the button writes a sentence with both.

👀 Show the solution
🌐 Solution
HTML
<input id="name" placeholder="Your name">
<input id="age" type="number" placeholder="Your age">
<button id="go">Introduce me</button>
<p id="res"></p>
CSS
body { font-family: Tahoma, sans-serif; }
button { font-size: 20px; padding: 8px 20px; }
JavaScript
document.getElementById("go").onclick = function () {
  const name = document.getElementById("name").value;
  const age = Number(document.getElementById("age").value);
  document.getElementById("res").textContent = name + " is " + age + ", and in a year he will be " + (age + 1);
};
🖥️ The page in Robo Web:The page in Robo Web

Result: Youssef is 11, and in a year he will be 12

Finished the lesson? Mark it so your progress counts.