Categories
JavaScript Answers

How to use Chrome Fullscreen API with JavaScript?

Sometimes, we want to use Chrome Fullscreen API with JavaScript.

In this article, we’ll look at how to use Chrome Fullscreen API with JavaScript.

How to use Chrome Fullscreen API with JavaScript?

To use Chrome Fullscreen API with JavaScript, we use a few properties in an element.

For instance, we write

const toggleFullScreen = () => {
  if (!document.fullscreenElement) {
    document.documentElement.requestFullscreen();
  } else {
    document.exitFullscreen();
  }
};

to define the toggleFullScreen function.

In it, we check if the document isn’t full screen with document.fullscreenElement.

If it’s false, we request it to be full screen with document.documentElement.requestFullscreen.

Otherwise, we call document.exitFullscreen to exit full screen mode.

Conclusion

To use Chrome Fullscreen API with JavaScript, we use a few properties in an element.

Categories
JavaScript Answers

How to push to an array only if value doesn’t exist with JavaScript?

Sometimes, we want to push to an array only if value doesn’t exist with JavaScript.

In this article, we’ll look at how to push to an array only if value doesn’t exist with JavaScript.

How to push to an array only if value doesn’t exist with JavaScript?

To push to an array only if value doesn’t exist with JavaScript, we use a set.

For instance, we write

const s = new Set();

s.add("hello");
s.add("world");
s.add("hello");
s.delete("world");

const array = Array.from(s);

to use the Set constructor to create a set.

Then we call add to add entries to it.

And we call delete to delete entries from it.

Next, we call Array.from to convert set s to an array.

Conclusion

To push to an array only if value doesn’t exist with JavaScript, we use a set.

Categories
JavaScript Answers

How to remove all series data from a HighCharts chart with JavaScript?

Sometimes, we want to remove all series data from a HighCharts chart with JavaScript.

In this article, we’ll look at how to remove all series data from a HighCharts chart with JavaScript.

How to remove all series data from a HighCharts chart with JavaScript?

To remove all series data from a HighCharts chart with JavaScript, we call the remove method.

For instance, we write

while (chart.series.length > 0) {
  chart.series[0].remove(true);
}

to loop through the chart series with a while loop.

Then we get the first one with chart.series[0].

And we call remove with true to remove it.

Conclusion

To remove all series data from a HighCharts chart with JavaScript, we call the remove method.

Categories
JavaScript Answers

How to hide the Android keyboard using JavaScript?

Sometimes, we want to hide the Android keyboard using JavaScript.

In this article, we’ll look at how to hide the Android keyboard using JavaScript.

How to hide the Android keyboard using JavaScript?

To hide the Android keyboard using JavaScript, we set hide it after we focus on the input.

For instance, we write

const field = document.createElement("input");
field.setAttribute("type", "text");
document.body.appendChild(field);

setTimeout(() => {
  field.focus();
  setTimeout(() => {
    field.setAttribute("style", "display: none;");
  }, 50);
}, 50);

to call field.focus to focus on the input.

Then we call field.setAttribute with 'style' and 'display: none' to hide the field.

We do both with a delay by putting them in a setTimeout callback.

Conclusion

To hide the Android keyboard using JavaScript, we set hide it after we focus on the input.

Categories
JavaScript Answers

How to set an option’s selected attribute from dynamic created option with JavaScript?

Sometimes, we want to set an option’s selected attribute from dynamic created option with JavaScript/

In this article, we’ll look at how to set an option’s selected attribute from dynamic created option with JavaScript.

How to set an option’s selected attribute from dynamic created option with JavaScript?

To set an option’s selected attribute from dynamic created option with JavaScript, we set the selected property of the option.

For instance, we write

const country = document.getElementById("country");
country.options[country.options.selectedIndex].selected = true;

to select the select element with getElementById.

And we get the selected option with country.options[country.options.selectedIndex]

We set its selected property to true to make it selected.

Conclusion

To set an option’s selected attribute from dynamic created option with JavaScript, we set the selected property of the option.