Categories
JavaScript Answers

How to insert a large block of HTML in JavaScript?

Sometimes, we want to insert a large block of HTML in JavaScript.

In this article, we’ll look at how to insert a large block of HTML in JavaScript.

How to insert a large block of HTML in JavaScript?

To insert a large block of HTML in JavaScript, we use template literals.

For instance, we write

const a = 1;
const b = 2;
const div = document.createElement("div");
div.setAttribute("class", "post block bc2");
div.innerHTML = `
    <div class="parent">
        <div class="child">${a}</div>
        <div class="child">+</div>
        <div class="child">${b}</div>
        <div class="child">=</div>
        <div class="child">${a + b}</div>
    </div>
`;
document.getElementById("posts").appendChild(div);

to create the div with createElement.

Then we set its class attribute with setAttribute.

Next we set its content by setting the innerHTML property to the HTML we want to render.

We use template literals to interpolate values in the HTML.

And then we call appendChild to append the div as the last child of the element with ID posts.

Conclusion

To insert a large block of HTML in JavaScript, we use template literals.

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.