Categories
JavaScript Answers

How to get the information from a meta tag with JavaScript?

To get the information from a meta tag with JavaScript, we use the content property.

For instance, we write

const content = document.head.querySelector(
  "[property~=video][content]"
).content;

to select the meta element with the property attribute containing video and has the content attribute with document.head..querySelector from the head element.

Then we get the content attribute from the selected element with the content property.

Categories
JavaScript Answers

How to get the element clicked for the whole document with JavaScript?

To get the element clicked for the whole document with JavaScript, we add a click event handler for document.

For instance, we write

document.addEventListener(
  "click",
  (e) => {
    const target = e.target;
    const text = target.textContent;
  },
  false
);

to add a click handler for document with addEventListener.

In the click handler, we get the element that we clicked on with e.target.

And then we get the element’s text content with textContent.

Categories
JavaScript Answers

How to change the playing speed of videos in JavaScript?

To change the playing speed of videos in JavaScript, we set the defaultPlaybackRate and playbackRate properties.

For instance, we write

document.querySelector("video").defaultPlaybackRate = 2.0;
document.querySelector("video").play();

to select the video element with querySelector.

Then we set its default playback rate to 2 times the original speed by setting the defaultPlaybackRate property to 2.0.

Or we write

document.querySelector("video").playbackRate = 3.0;

to set the video’s playbackRate to 3 times the original speed.

Categories
JavaScript Answers

How to enable Bootstrap tooltip on disabled button with HTML?

To enable Bootstrap tooltip on disabled button with HTML, we wrap the disabled button with a wrapper element.

For instance, we write

<div class="tooltip-wrapper" data-title="hello world">
  <button class="btn btn-default" disabled>button disabled</button>
</div>

to wrap a div with the data-title attribute with the tooltip text with class tooltip-wrapper around the disabled button to show the tooltip when hovering over the disabled button.

Categories
JavaScript Answers

How to create a string of variable length, filled with a repeated character with JavaScript?

To create a string of variable length, filled with a repeated character with JavaScript, we call the array join method.

For instance, we write

const str = new Array(len + 1).join(character);

to create an array with length len + 1 with Array.

And then we call join with character to return a string filled with character of length len.