Exercise 19: Set Element InnerHTML Safely

Problem Statement

Function `setInnerHTML(el, html)` banao jo element ka innerHTML set kare with validation. Example: setInnerHTML(divEl, "<p>Hello</p>")

Sample Output:

setInnerHTML(divEl, "<p>Hello</p>")
// Sets innerHTML to provided HTML string

Solution

const setInnerHTML = (el, html) => {
  if (el && typeof html === "string") el.innerHTML = html;
};

Explanation

Overall Goal:

  • Element ka innerHTML safely set karna.

Real world:

  • Dynamic content: HTML injection.
  • Content updates: HTML updates.