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.

Categories
JavaScript Answers

How to dynamically change a web page’s title with JavaScript?

Sometimes, we want to dynamically change a web page’s title with JavaScript.

In this article, we’ll look at how to dynamically change a web page’s title with JavaScript.

How to dynamically change a web page’s title with JavaScript?

To dynamically change a web page’s title with JavaScript, we set its document.title property.

For instance, we write

document.title = "test";

to set the document.title property to 'test' to make the page title ‘test’.

Conclusion

To dynamically change a web page’s title with JavaScript, we set its document.title property.

Categories
JavaScript Answers

How to invoke JavaScript code in an iframe from the parent page?

Sometimes, we want to invoke JavaScript code in an iframe from the parent page.

In this article, we’ll look at how to invoke JavaScript code in an iframe from the parent page.

How to invoke JavaScript code in an iframe from the parent page?

to invoke JavaScript code in an iframe from the parent page, we get the function from the contentWindow property.

For instance, we write

document.getElementById("targetFrame").contentWindow.targetFunction();

to get the iframe with getElementById.

Then we call the targetFunction function in the iframe after getting it from the contentWindow property.

Conclusion

to invoke JavaScript code in an iframe from the parent page, we get the function from the contentWindow property.