Learn with Yasir

Share Your Feedback

Real World JavaScript Examples for Beginners – Practical Coding Guide


Explore real-world JavaScript examples tailored for beginners. Learn how to build dynamic web features with hands-on code snippets, including DOM manipulation, form validation, and interactive web apps.

Contents

🛠️ Dynamically Change Style with JavaScript

🎨 1. Change Text Color on Button Click

  • Change text color interactively using JavaScript.
<p id="text">Click the button to change my color!</p>
<button onclick="changeColor()">Change Color</button>

<script>
  function changeColor() {
    document.getElementById("text").style.color = "blue";
  }
</script>

🧠 Used in interactive stories, quizzes, or themed messages.


🔠 2. Change Font Size Dynamically

  • Let users increase font size for better readability.
<p id="resizeText">Resize me!</p>
<button onclick="increaseFont()">Increase Font</button>

<script>
  function increaseFont() {
    document.getElementById("resizeText").style.fontSize = "2em";
  }
</script>

🧠 Helpful in accessibility or reading mode features.


🌈 3. Change Background Color Randomly

  • Make it fun with a random color generator!
<button onclick="randomBg()">Random Background</button>

<script>
  function randomBg() {
    const colors = ["#f8b400", "#00b894", "#6c5ce7", "#d63031"];
    const color = colors[Math.floor(Math.random() * colors.length)];
    document.body.style.backgroundColor = color;
  }
</script>

🧠 Used in color pickers, design tools, and fun quizzes.

🔹 const

Used to declare variables that won’t be reassigned. It ensures the variable reference stays constant (but not necessarily the content if it’s an object or array).

const colors = ["red", "green", "blue"];

📌 Use const when you don’t plan to reassign the variable.


🔹 Math.random()

Generates a random decimal number between 0 (inclusive) and 1 (exclusive).

Math.random(); // Example: 0.462

📌 Often used to create randomness in games or styles.


🔹 Math.floor()

Rounds a decimal number down to the nearest whole number.

Math.floor(4.9); // Returns 4

📌 Used here to select a random index from the colors array.


🔗 For a detailed explanation, see::


✏️ 4. Highlight Form Field on Focus

  • Improve form UX by highlighting active fields.
<input type="text" id="name" onfocus="highlight(this)" onblur="removeHighlight(this)">

<script>
  function highlight(element) {
    element.style.backgroundColor = "#ffffcc";
  }

  function removeHighlight(element) {
    element.style.backgroundColor = "";
  }
</script>

🧠 Used to improve form UX on signup/login pages.


👁️ 5. Toggle Visibility (Show/Hide Div)

  • Show or hide sections like FAQs or dropdowns.
<div id="info" style="display:none;">
  This is some hidden information.
</div>
<button onclick="toggleInfo()">Show/Hide Info</button>

<script>
  function toggleInfo() {
    const div = document.getElementById("info");
    div.style.display = div.style.display === "none" ? "block" : "none";
  }
</script>

🧠 Great for FAQs, dropdowns, and read-more sections.


🧪 6. Add or Remove CSS Class

  • Use class toggling for cleaner and reusable styles.
<p id="para">Watch me get fancy!</p>
<button onclick="toggleFancy()">Toggle Style</button>

<style>
  .fancy {
    font-weight: bold;
    color: red;
    text-transform: uppercase;
  }
</style>

<script>
  function toggleFancy() {
    document.getElementById("para").classList.toggle("fancy");
  }
</script>

🧠 Modern and scalable method for styling elements dynamically.


🌗 7. Change Button Text and Style on Click

<button id="modeBtn" onclick="switchMode()">Enable Dark Mode</button>

<script>
  function switchMode() {
    const btn = document.getElementById("modeBtn");
    document.body.classList.toggle("dark");

    if (document.body.classList.contains("dark")) {
      btn.textContent = "Disable Dark Mode";
      btn.style.backgroundColor = "black";
      btn.style.color = "white";
    } else {
      btn.textContent = "Enable Dark Mode";
      btn.style.backgroundColor = "";
      btn.style.color = "";
    }
  }
</script>

<style>
  .dark {
    background-color: #222;
    color: #eee;
  }
</style>

🧠 Useful in websites with themes or accessibility options.


More Examples

🔐 8. Show/Hide Password Field

Use Case: Toggle visibility of a password input.

<input type="password" id="password">
<button onclick="togglePassword()">Show/Hide</button>

<script>
  function togglePassword() {
    const pwd = document.getElementById("password");
    pwd.type = pwd.type === "password" ? "text" : "password";
  }
</script>

🧠 Used in login/signup forms.


✍️ 9. Live Character Counter

Use Case: Show how many characters are typed in a form input.

<textarea id="msg" onkeyup="countChar()"></textarea>
<p>Characters: <span id="count">0</span></p>

<script>
  function countChar() {
    const text = document.getElementById("msg").value;
    document.getElementById("count").textContent = text.length;
  }
</script>

🧠 Useful for Twitter/X post limit, feedback forms, etc.


⏰ 10. Greeting Based on Time

Use Case: Personalize greeting on websites.

<p id="greet"></p>

<script>
  const hour = new Date().getHours();
  let greeting;

  if (hour < 12) {
    greeting = "Good Morning!";
  } else if (hour < 18) {
    greeting = "Good Afternoon!";
  } else {
    greeting = "Good Evening!";
  }

  document.getElementById("greet").textContent = greeting;
</script>

🧠 Used on dashboards, landing pages, e-commerce portals.


➕ 11. Simple Calculator

Use Case: Basic operations using form and JS.

<input type="number" id="num1"> +
<input type="number" id="num2">
<button onclick="calculate()">=</button>
<span id="result"></span>

<script>
  function calculate() {
    let a = parseFloat(document.getElementById("num1").value);
    let b = parseFloat(document.getElementById("num2").value);
    document.getElementById("result").textContent = a + b;
  }
</script>

🧠 Great intro to arithmetic and DOM.


🌙 12. Dark Mode Toggle

Use Case: Switch between light and dark theme.

<button onclick="toggleTheme()">Toggle Dark Mode</button>

<script>
  function toggleTheme() {
    document.body.classList.toggle("dark-mode");
  }
</script>

<style>
  .dark-mode {
    background-color: black;
    color: white;
  }
</style>

🧠 Used on most modern websites for better user experience.


🚫 13. Disable Button After Click

Use Case: Prevent multiple form submissions.

<button onclick="submitForm(this)">Submit</button>

<script>
  function submitForm(btn) {
    btn.disabled = true;
    btn.textContent = "Submitted!";
  }
</script>

🧠 Common in forms and transactions.


🕒 14. Real-Time Clock

Use Case: Display the current time live.

<p id="clock"></p>

<script>
  function updateClock() {
    const now = new Date().toLocaleTimeString();
    document.getElementById("clock").textContent = now;
  }

  setInterval(updateClock, 1000);
</script>

🧠 Great for dashboards or digital clocks.



🧠 Practice & Progress

Explore More Topics