Categories
JavaScript Answers

How to add CSS animation on click with JavaScript?

Sometimes, we want to add CSS animation on click with JavaScript.

In this article, we’ll look at how to add CSS animation on click with JavaScript.

How to add CSS animation on click with JavaScript?

To add CSS animation on click with JavaScript, we add animation effects for a class.

For instance, we write

.classname {
  -webkit-animation-name: cssAnimation;
  -webkit-animation-duration: 3s;
  -webkit-animation-iteration-count: 1;
  -webkit-animation-timing-function: ease;
  -webkit-animation-fill-mode: forwards;
}

@-webkit-keyframes cssAnimation {
  from {
    -webkit-transform: rotate(0deg) scale(1) skew(0deg) translate(100px);
  }
  to {
    -webkit-transform: rotate(0deg) scale(2) skew(0deg) translate(100px);
  }
}

to add animation to the classname class.

In it, we add the keyframes for the animation.

Then we write

const ani = () => {
  document.getElementById("img").className = "classname";
};

to define the ani function that selects the element to animate with getElementById.

And we set its className to 'classname' to apply the animation effects that’s applied for the class.

Conclusion

To add CSS animation on click with JavaScript, we add animation effects for a class.

Categories
JavaScript Answers

How to make a checkbox read only with HTML and JavaScript?

Sometimes, we want to make a checkbox read only with HTML and JavaScript.

In this article, we’ll look at how to make a checkbox read only with HTML and JavaScript.

How to make a checkbox read only with HTML and JavaScript?

To make a checkbox read only with HTML and JavaScript, we return false when we click on the checkbox.

For instance, we write

<input type="checkbox" onclick="return false" />

to set onclick to return false` to stop clicks from modifying the checkbox’s state.

Conclusion

To make a checkbox read only with HTML and JavaScript, we return false when we click on the checkbox.

Categories
JavaScript Answers

How to load HTML file contents to div with JavaScript?

Sometimes, we want to load HTML file contents to div with JavaScript.

In this article, we’ll look at how to load HTML file contents to div with JavaScript.

How to load HTML file contents to div with JavaScript?

To load HTML file contents to div with JavaScript, we use fetch.

For instance, we write

const loadHtml = async () => {
  const response = await fetch("page.html");
  const text = await response.text();
  document.getElementById("elementID").insertAdjacentText("beforeend", text);
};

loadHtml();

to define the loadHtml function.

In it, we call fetch to get the content of page.html.

We get it as a string with response.text.

And then we call insertAdjacentText to insert the HTMLK into the element we selected with getElementById.

Conclusion

To load HTML file contents to div with JavaScript, we use fetch.

Categories
JavaScript Answers

How to find prime numbers between 0 – 100 with JavaScript?

Sometimes, we want to find prime numbers between 0 – 100 with JavaScript.

In this article, we’ll look at how to find prime numbers between 0 – 100 with JavaScript.

How to find prime numbers between 0 – 100 with JavaScript?

To find prime numbers between 0 – 100 with JavaScript, we use a for loop.

For instance, we write

const isPrime = (n) => {
  if (n < 2) {
    return false;
  }

  const q = Math.floor(Math.sqrt(n));

  for (let i = 2; i <= q; i++) {
    if (n % i === 0) {
      return false;
    }
  }

  return true;
};

to define the isPrime function.

In it, we return false if n is less than 2.

Otherwise, we loop from 2 to the floor of the square root of n.

And we check if each value i is evenly divides n.

If that’s the case, we return false.

Otherwise, we return true.

Conclusion

To find prime numbers between 0 – 100 with JavaScript, we use a for loop.

Categories
JavaScript Answers

How to toggle class using pure JavaScript in HTML?

Sometimes, we want to toggle class using pure JavaScript in HTML.

In this article, we’ll look at how to toggle class using pure JavaScript in HTML.

How to toggle class using pure JavaScript in HTML?

To toggle class using pure JavaScript in HTML, we use the classList property.

For instance, we write

const container = document.querySelector("#container");

container.addEventListener("mouseenter", (e) => {
  e.target.classList.remove("first");
  e.target.classList.add("second");
});

container.addEventListener("mouseleave", (e) => {
  e.target.classList.add("first");
  e.target.classList.remove("second");
});

to select the element with querySelector.

Then we call addEventListener to listen to the mouseenter and `mouseleave events.

In the listeners, we call e.target.classList.remove to remove the class from the current element.

And we use e.target.classList.add to add a class to the current element.

Conclusion

To toggle class using pure JavaScript in HTML, we use the classList property.