Categories
JavaScript Answers

How to get HTML5 video element dimensions with JavaScript?

Sometimes, we want to get HTML5 video element dimensions with JavaScript.

In this article, we’ll look at how to get HTML5 video element dimensions with JavaScript.

How to get HTML5 video element dimensions with JavaScript?

To get HTML5 video element dimensions with JavaScript, we can use the videoHeight and videoWidth properties.

For instance, we write

<video id="foo" src="foo.mp4"></video>

to add a video element.

Then we write

const vid = document.getElementById("foo");
const height = vid.videoHeight;
const width = vid.videoWidth;

to select the video element with getElementById.

And then we get the intrinsic video height and width with videoHeight and videoWidth.

Conclusion

To get HTML5 video element dimensions with JavaScript, we can use the videoHeight and videoWidth properties.

Categories
JavaScript Answers

How to fix Bootstrap 4 drop down menu is not working with JavaScript?

Sometimes, we want to fix Bootstrap 4 drop down menu is not working with JavaScript.

In this article, we’ll look at how to fix Bootstrap 4 drop down menu is not working with JavaScript.

How to fix Bootstrap 4 drop down menu is not working with JavaScript?

To fix Bootstrap 4 drop down menu is not working with JavaScript, we add popper.js before loading Bootstrap.

For instance, we write

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js"></script>
<link
  rel="stylesheet"
  href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css"
/>

to add the popper.js script tag before the Bootstrap 4 script tags.

Conclusion

To fix Bootstrap 4 drop down menu is not working with JavaScript, we add popper.js before loading Bootstrap.

Categories
JavaScript Answers

How to replace all accented characters in a string with JavaScript?

Sometimes, we want to replace all accented characters in a string with JavaScript.

In this article, we’ll look at how to replace all accented characters in a string with JavaScript.

How to replace all accented characters in a string with JavaScript?

To replace all accented characters in a string with JavaScript, we can use the string replace method with a regex and callback.

For instance, we write

const translate = {
  ä: "a",
  ö: "o",
  ü: "u",
  Ä: "A",
  Ö: "O",
  Ü: "U",
};
const translateRe = /[öäüÖÄÜ]/g;
return s.replace(translateRe, (match) => {
  return translate[match];
});

to create the translateRe regex to match all instances of accented characters we want to replace.

Then we call s.replace with translateRe and a callback that returns the replacement character for each matched character.

Conclusion

To replace all accented characters in a string with JavaScript, we can use the string replace method with a regex and callback.

Categories
React Answers

How to set the default route to another Route in React Router?

Sometimes, we want to set the default route to another Route in React Router.

In this article, we’ll look at how to set the default route to another Route in React Router.

How to set the default route to another Route in React Router?

To set the default route to another Route in React Router, we can use the Navigate component.

For instance, we write

<Routes>
  <Route path="/" element={<Navigate to="/searchDashboard" />}>
    <Route path="searchDashboard" element={<SearchDashboard />} />
    <Route path="*" element={<Navigate to="/" />} />
  </Route>
</Routes>

to set the route for path / to navigate to the /searchDashboard route.

Likewise, we have the * catch all route that does the same thing.

And /searchDashboard renders the SearchDashboard component.

Conclusion

To set the default route to another Route in React Router, we can use the Navigate component.

Categories
JavaScript Answers

How to create a unique number with JavaScript time?

Sometimes, we want to create a unique number with JavaScript time.

In this article, we’ll look at how to create a unique number with JavaScript time.

How to create a unique number with JavaScript time?

To create a unique number with JavaScript time, we can add the timestamp of the current datetime to a random number.

For instance, we write

const n = Date.now() + Math.random();

to add the timestamp of the current datetime that we get with Date.now to a random number returned by Math.random.

Conclusion

To create a unique number with JavaScript time, we can add the timestamp of the current datetime to a random number.