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.

Categories
JavaScript Answers

How to close current tab in a browser window with JavaScript?

Sometimes, we want to close current tab in a browser window with JavaScript.

In this article, we’ll look at how to close current tab in a browser window with JavaScript.

How to close current tab in a browser window with JavaScript?

To close current tab in a browser window with JavaScript, we call the window.close method.

For instance, we write

window.close();

to close the current tab.

Conclusion

To close current tab in a browser window with JavaScript, we call the window.close method.

Categories
JavaScript Answers

How to programmatically set the value of a select box element using JavaScript?

To programmatically set the value of a select box element using JavaScript, we set its value property.

For instance, we write

<select id="leaveCode" name="leaveCode">
  <option value="10">Annual Leave</option>
  <option value="11">Medical Leave</option>
  <option value="14">Long Service</option>
  <option value="17">Leave Without Pay</option>
</select>

to add the select drop down.

Then we write

const element = document.getElementById("leaveCode");
element.value = "11";

to select the drop down with getElementById.

And we set its value property to the value attribute of the option we want to select.