Categories
JavaScript Answers

How to append text to a div with JavaScript?

Sometimes, we want to append text to a div with JavaScript.

In this article, we’ll look at how to append text to a div with JavaScript.

How to append text to a div with JavaScript?

To append text to a div with JavaScript, we append the text to the innerHTML string.

For instance, we write

const div = document.getElementById("divID");
div.innerHTML += "Extra stuff";

to select the div with getElementById.

Then we append the new text by concatenating it to the innerHTML string.

Conclusion

To append text to a div with JavaScript, we append the text to the innerHTML string.

Categories
JavaScript Answers

How to detect div dimension change with JavaScript?

Sometimes, we want to detect div dimension change with JavaScript./

In this article, we’ll look at how to detect div dimension change with JavaScript.

How to detect div dimension change with JavaScript?

To detect div dimension change with JavaScript, we use ResizeObserver.

For instance, we write

const outputSize = () => {
  console.log(textbox.offsetWidth, textbox.offsetHeight);
};
outputsize();

new ResizeObserver(outputSize).observe(textbox);

to create a ResizeObserver obbject with the outputSize function.

And we call observer with the textbox element to watch its size change.

We get the width and height with offsetWidth and offsetHeight.

Conclusion

To detect div dimension change with JavaScript, we use ResizeObserver.

Categories
JavaScript Answers

How to delete a localStorage item when the browser window/tab is closed with JavaScript?

Sometimes, we want to delete a localStorage item when the browser window/tab is closed with JavaScript.

In this article, we’ll look at how to delete a localStorage item when the browser window/tab is closed with JavaScript.

How to delete a localStorage item when the browser window/tab is closed with JavaScript?

To delete a localStorage item when the browser window/tab is closed with JavaScript, we call removeItem.

For instance, we write

window.onbeforeunload = () => {
  window.localStorage.removeItem("keyName");
};

to set window.onbeforeunload to a function that calls localStorage.removeItem with the key of the item to remove.

onbeforeunload is called before we close the tab or window.

Conclusion

To delete a localStorage item when the browser window/tab is closed with JavaScript, we call removeItem.

Categories
JavaScript Answers

How to open URL in same window and in same tab with JavaScript?

Sometimes, we want to open URL in same window and in same tab with JavaScript.

In this article, we’ll look at how to open URL in same window and in same tab with JavaScript.

How to open URL in same window and in same tab with JavaScript?

To open URL in same window and in same tab with JavaScript, we call window.open with '_self'.

For instance, we write

window.open("https://www.example.com", "_self");

to call window.open to open https://www.example.com in the same window or tab with '_self'.

Conclusion

To open URL in same window and in same tab with JavaScript, we call window.open with '_self'.

Categories
JavaScript Answers

How to change the text of a span element using JavaScript?

Sometimes, we want to change the text of a span element using JavaScript.

In this article, we’ll look at how to change the text of a span element using JavaScript.

How to change the text of a span element using JavaScript?

To change the text of a span element using JavaScript, we change its textContent property.

For instance, we write

document.getElementById("myspan").textContent = "new text";

to select the span with getElementById.

Then we set its textContent property to change the text.

Conclusion

To change the text of a span element using JavaScript, we change its textContent property.