Categories
JavaScript Answers

How to set radio button status with JavaScript?

Sometimes, we want to set radio button status with JavaScript.

In this article, we’ll look at how to set radio button status with JavaScript.

How to set radio button status with JavaScript?

To set radio button status with JavaScript, we set the checked property of the radio button.

For instance, we write

const radioBtn = document.getElementById("theid");
radioBtn.checked = true;

to select the radio button with getElementById.

Then we set the radioBtn.checked property to true to select the radio button.

Conclusion

To set radio button status with JavaScript, we set the checked property of the radio button.

Categories
JavaScript Answers

How to change onclick action with a JavaScript function?

Sometimes, we want to change onclick action with a JavaScript function.

In this article, we’ll look at how to change onclick action with a JavaScript function.

How to change onclick action with a JavaScript function?

To change onclick action with a JavaScript function, we can set the element’s onclick property to a function that runs when we click on the element.

For instance, we write

document.getElementById(`${id}-button`).onclick = () => {
  hideError(id);
};

to select the element with getElementById.

Then we set its onclick property to a function that calls the hideError function.

Conclusion

To change onclick action with a JavaScript function, we can set the element’s onclick property to a function that runs when we click on the element.

Categories
JavaScript Answers

How to get JavaScript select box’s selected text?

Sometimes, we want to get JavaScript select box’s selected text.

In this article, we’ll look at how to get JavaScript select box’s selected text.

How to get JavaScript select box’s selected text?

To get JavaScript select box’s selected text, we can use the select element’s options and selectedIndex properties.

For instance, we write

const sel = document.getElementById("selectBox");
const text = sel.options[sel.selectedIndex].text;

to select the select element with getElementById.

Then we get the index of the selected option with sel.selectedIndex.

Next we use that with sel.options to get the option that’s selected.

And then get the text of the option with the text property.

Conclusion

To get JavaScript select box’s selected text, we can use the select element’s options and selectedIndex properties.

Categories
JavaScript Answers

How to write formatted JSON in Node.js?

Sometimes, we want to write formatted JSON in Node.js.

In this article, we’ll look at how to write formatted JSON in Node.js.

How to write formatted JSON in Node.js?

To write formatted JSON in Node.js, we can use the JSON.stringify method.

For instance, we write

const output = JSON.parse(json);
const printToFile = JSON.stringify(output, null, "\t");

to call JSON.parse to parse the json JSON string into an object.

Then we call JSON.stringigfy with output and '\t' to return a JSON string that has indentation with tabs.

Conclusion

To write formatted JSON in Node.js, we can use the JSON.stringify method.

Categories
JavaScript Answers

How to calculate page load time in JavaScript?

Sometimes, we want to calculate page load time in JavaScript.

In this article, we’ll look at how to calculate page load time in JavaScript.

How to calculate page load time in JavaScript?

To calculate page load time in JavaScript, we can use properties in the window.performance.timing property.

For instance, we write

const loadTime =
  window.performance.timing.domContentLoadedEventEnd -
  window.performance.timing.navigationStart;

window.performance.timing.domContentLoadedEventEnd is the timestamp when DOM finished loading.

And window.performance.timing.navigationStart is the timestamp when navigation started.

We subtract window.performance.timing.domContentLoadedEventEnd by window.performance.timing.navigationStart to get the page load time in milliseconds.

Conclusion

To calculate page load time in JavaScript, we can use properties in the window.performance.timing property.