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.

Categories
JavaScript Answers

How to track onchange when typing in text input with JavaScript?

Sometimes, we want to track onchange when typing in text input with JavaScript.

In this article, we’ll look at how to track onchange when typing in text input with JavaScript.

How to track onchange when typing in text input with JavaScript?

To track onchange when typing in text input with JavaScript, we listen to the input event.

For instance, we write

const source = document.getElementById("source");

const inputHandler = (e) => {
  console.log(e.target.value);
};

source.addEventListener("input", inputHandler);

to select the input with getElementById.

Then we call addEventListener to listen for the input event in the input.

inputHandler is called when we change the input’s value.

Conclusion

To track onchange when typing in text input with JavaScript, we listen to the input event.