Categories
JavaScript Answers

How to check if an element is a child of a parent with JavaScript?

Sometimes, we want to check if an element is a child of a parent with JavaScript.

In this article, we’ll look at how to check if an element is a child of a parent with JavaScript.

How to check if an element is a child of a parent with JavaScript?

To check if an element is a child of a parent with JavaScript, we can use the parent element’s contains method.

For instance, we write

const contains = (parent, child) => {
  return parent !== child && parent.contains(child);
};

to create the contains function that checks if parent isn’t child and that the parent element contains the child element with parent.contains.

Conclusion

To check if an element is a child of a parent with JavaScript, we can use the parent element’s contains method.

Categories
JavaScript Answers

How to fix HTML5 canvas ctx.fillText not adding line breaks with JavaScript?

Sometimes, we want to fix HTML5 canvas ctx.fillText not adding line breaks with JavaScript.

In this article, we’ll look at how to fix HTML5 canvas ctx.fillText not adding line breaks with JavaScript.

How to fix HTML5 canvas ctx.fillText not adding line breaks with JavaScript?

To fix HTML5 canvas ctx.fillText not adding line breaks with JavaScript, we set the location of the text when we call fillText.

For instance, we write

const c = document.getElementById("c").getContext("2d");
c.font = "11px Courier";
console.log(c);
const txt = "line 1\nline 2\nthird line..";
const x = 30;
const y = 30;
const lineHeight = 15;
const lines = txt.split("\n");

for (const line of lines) {
  c.fillText(line, x, y + i * lineHeight);
}

to loop through the lines with a for-of loop.

In it, we call the context’s fillText method with the line to render.

Then y coordinates of the text is set to y + i * lineHeight which positions the text below the previous line.

Conclusion

To fix HTML5 canvas ctx.fillText not adding line breaks with JavaScript, we set the location of the text when we call fillText.

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.