Categories
JavaScript Answers

How to check if data attribute exist with plain JavaScript?

Sometimes, we want to check if data attribute exist with plain JavaScript.

In this article, we’ll look at how to check if data attribute exist with plain JavaScript.

How to check if data attribute exist with plain JavaScript?

To check if data attribute exist with plain JavaScript, we use the hasAttribute method.

For instance, we write

if (!object.hasAttribute("data-example-param")) {
  // ...
}

to check if object doesn’t have the data-example-param property.

hasAttribute returns true if the property with the given name exists and false otherwise.

Conclusion

To check if data attribute exist with plain JavaScript, we use the hasAttribute method.

Categories
JavaScript Answers

How to fix timeout feature in the axios library is not working with JavaScript?

Sometimes, we want to fix timeout feature in the axios library is not working with JavaScript.

In this article, we’ll look at how to fix timeout feature in the axios library is not working with JavaScript.

How to fix timeout feature in the axios library is not working with JavaScript?

To fix timeout feature in the axios library is not working with JavaScript, we use a cancel token.

For instance, we write

const source = CancelToken.source();
const timeout = setTimeout(() => {
  source.cancel();
}, 10000);

const result = await axios.get(ip + "/config", { cancelToken: source.token });
clearTimeout(timeout);

to call axios.get with the cancelToken we created with CancelToken.source.

Then we source.cancel in the setTimeout callback to cancel the get request when the request hasn’t finished in 10 seconds.

If the request is done before then, we use clearTimeout(timeout); to clear the timer.

Conclusion

To fix timeout feature in the axios library is not working with JavaScript, we use a cancel token.

Categories
JavaScript Answers

How to call JavaScript function in an iframe?

Sometimes, we want to call JavaScript function in an iframe.

In this article, we’ll look at how to call JavaScript function in an iframe.

How to call JavaScript function in an iframe?

To call JavaScript function in an iframe, we get the window object of the iframe with the contentWindow property.

For instance, we write

document.getElementById("resultFrame").contentWindow.Reset();

to select the iframe with getElementById.

Then we call the contentWindow.Reset global function in the iframe.

contentWindow is the window object of the iframe.

Conclusion

To call JavaScript function in an iframe, we get the window object of the iframe with the contentWindow property.

Categories
JavaScript Answers

How to clear radio button in JavaScript?

Sometimes, we want to clear radio button in JavaScript.

In this article, we’ll look at how to clear radio button in JavaScript.

How to clear radio button in JavaScript?

To clear radio button in JavaScript, we set its checked property to false.

For instance, we write

document.querySelector('input[name="Choose"]:checked').checked = false;

to select the checkbox we want to uncheck with querySelector.

And then we set its checked property to false to clear it.

Conclusion

To clear radio button in JavaScript, we set its checked property to false.

Categories
JavaScript Answers

How to get margin value of a div in plain JavaScript?

Sometimes, we want to get margin value of a div in plain JavaScript.

In this article, we’ll look at how to get margin value of a div in plain JavaScript.

How to get margin value of a div in plain JavaScript?

To get margin value of a div in plain JavaScript, we use the getComputedStyle method.

For instance, we write

const p = document.getElementById("target");
const style = window.getComputedStyle(p);

console.log(style.marginTop);

to select the element we want to get the margin for with getElementById.

Then we call getComputedStyle with p to return an object with the computed styles.

And then we get the margin top value with style.marginTop in pixels.

Conclusion

To get margin value of a div in plain JavaScript, we use the getComputedStyle method.