Categories
JavaScript Answers

How to get value of selected radio button with JavaScript?

Sometimes, we want to get value of selected radio button with JavaScript.

In this article, we’ll look at how to get value of selected radio button with JavaScript.

How to get value of selected radio button with JavaScript?

To get value of selected radio button with JavaScript, we use querySelector.

For instance, we write

const value = document.querySelector('input[name="rate"]:checked').value;

to select the radio button with name rate that’s selected with :checked and querySelector.

And we get its value attribute value with value.

Conclusion

To get value of selected radio button with JavaScript, we use querySelector.

Categories
JavaScript Answers

How to create a style tag tag with JavaScript?

Sometimes, we want to create a style tag tag with JavaScript.

In this article, we’ll look at how to create a style tag tag with JavaScript.

How to create a style tag tag with JavaScript?

To create a style tag tag with JavaScript, we call createElement.

For instance, we write

const ss = document.createElement("link");
ss.type = "text/css";
ss.rel = "stylesheet";
ss.href = "style.css";
document.head.appendChild(ss);

to call createElement to create a link element.

Then we set its attributes by setting its properties.

Next, we call document.head.appendChild to append the link element as the last child of the head element.

Conclusion

To create a style tag tag with JavaScript, we call createElement.

Categories
JavaScript Answers

How to change website favicon dynamically with JavaScript?

Sometimes, we want to change website favicon dynamically with JavaScript.

In this article, we’ll look at how to change website favicon dynamically with JavaScript.

How to change website favicon dynamically with JavaScript?

To change website favicon dynamically with JavaScript, we create a link element.

For instance, we write

const link = document.querySelector("link[rel~='icon']");
if (!link) {
  link = document.createElement("link");
  link.rel = "icon";
  document.head.appendChild(link);
}
link.href = "https://example.com/favicon.ico";

to select the link element with querySelector.

If it doesn’t exist, we create it with createElement.

And then we set its href property to the favicon’s URL.

Conclusion

To change website favicon dynamically with JavaScript, we create a link element.

Categories
JavaScript Answers

How to resize HTML5 canvas to fit window with JavaScript?

Sometimes, we want to resize HTML5 canvas to fit window with JavaScript.

In this article, we’ll look at how to resize HTML5 canvas to fit window with JavaScript.

How to resize HTML5 canvas to fit window with JavaScript?

To resize HTML5 canvas to fit window with JavaScript, we set the width and height properties.

For instance, we write

const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
ctx.canvas.width = window.innerWidth;
ctx.canvas.height = window.innerHeight;

to select the canvas with querySelector.

Then we get its context with getContext.

Next, we set its width and height to the window width and height, which we get from the innerWidth and innerHeight properties.

Conclusion

To resize HTML5 canvas to fit window with JavaScript, we set the width and height properties.

Categories
JavaScript Answers

How to listen for changes in contenteditable elements with JavaScript?

Sometimes, we want to listen for changes in contenteditable elements with JavaScript.

In this article, we’ll look at how to listen for changes in contenteditable elements with JavaScript.

How to listen for changes in contenteditable elements with JavaScript?

To listen for changes in contenteditable elements with JavaScript, we listen for the input event.

For instance, we write

<div contenteditable="true" id="editor">Please type something in here</div>

to add a contenteditable div.

Then we write

document.getElementById("editor").addEventListener("input", () => {
  console.log("input event fired");
});

to select the div with getElementById.

And then we call addEventListener to listen to the input event on it.

When we change the div’s contents, then event handler is called.

Conclusion

To listen for changes in contenteditable elements with JavaScript, we listen for the input event.