Categories
JavaScript Answers

How to retrieve the display property of a DOM element?

Sometimes, we want to retrieve the display property of a DOM element.

In this article, we’ll look at how to retrieve the display property of a DOM element.

How to retrieve the display property of a DOM element?

To retrieve the display property of a DOM element, we can get it with the window.getComputedStyle method.

For instance, we write:

<div>
  hello
</div>

to add a div.

Then we write:

const div = document.querySelector('div')
const styles = window.getComputedStyle(div)
console.log(styles.display)

to select the div with querySelector.

Then we call window.getComputedStyle with div to return the styles object.

Then we get the display CSS property value with styles.display.

Conclusion

To retrieve the display property of a DOM element, we can get it with the window.getComputedStyle method.

Categories
JavaScript Answers

How to rotate a base 64 image by X degrees and return new base64 image with JavaScript?

Sometimes, we want to rotate a base 64 image by X degrees and return new base64 image with JavaScript.

In this article, we’ll look at how to rotate a base 64 image by X degrees and return new base64 image with JavaScript.

How to rotate a base 64 image by X degrees and return new base64 image with JavaScript?

To rotate a base 64 image by X degrees and return new base64 image with JavaScript, we can put the image in a canvas.

Then we do the rotation in the canvas and return the image.

For instance, we write:

<img>

to add an img element.

Then we write:

const img = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAARElEQVR42u3PMREAAAgEIE1u9DeDqwcN6FSmHmgRERERERERERERERERERERERERERERERERERERERERERERERERkYsFbE58nZm0+8AAAAAASUVORK5CYII='

const rotateBase64Image = (base64data) => {
  const canvas = document.createElement("canvas");
  const ctx = canvas.getContext("2d");
  const image = new Image();
  image.src = base64data;
  return new Promise(resolve => {
    image.onload = () => {
      ctx.translate(image.width, image.height);
      ctx.rotate(45 * Math.PI / 180);
      ctx.drawImage(image, 0, 0);
      resolve(canvas.toDataURL())
    };
  })
}

(async () => {
  const rotatedImg = await rotateBase64Image(img)
  const imgEl = document.querySelector('img')
  imgEl.src = rotatedImg
})()

to define the rotateBase64Image function to rotate the base64data image by 45 degrees.

In it, we create a canvas with `createElement.

Then we get its context with getContext.

Next, we create a new Image instance and set its src property to base64data to load the image.

Then when the image is loaded, image.onload is run.

In onload, we rotate the image with rotate.

And we draw the rotated image with drawImage.

Finally, we call resolve with the base64 string version of the rotated image that we get from canvas.toDataURL.

Next, in the async function, we get call rotateBaseImage to rotate img.

Then we select the img element with querySelector.

And then we set imgEl.src to the rotateImg string.

Now we see the pink square rotated 45 degrees.

Conclusion

To rotate a base 64 image by X degrees and return new base64 image with JavaScript, we can put the image in a canvas.

Then we do the rotation in the canvas and return the image.

Categories
JavaScript Answers

How to execute a JavaScript function on the first page load?

Sometimes, we want to execute a JavaScript function on the first page load.

In this article, we’ll look at how to execute a JavaScript function on the first page load.

How to execute a JavaScript function on the first page load?

To execute a JavaScript function on the first page load, we can set a flag in local storage is done.

Then we stop running the function on subsequent page loads if the flag exists.

For instance, we write:

window.onload = () => {
  if (localStorage.getItem("hasCodeRunBefore")) {
    return
  }
  console.log('hello')
  localStorage.setItem("hasCodeRunBefore", true);
}

to set window.onload to a function that checks if the local storage with key 'hasCodeRunBefor' exists.

If it doesn’t, then we run the console log and calls localStorage.setItem to set the 'hasCodeRunBefore' flag to true.

Conclusion

To execute a JavaScript function on the first page load, we can set a flag in local storage is done.

Then we stop running the function on subsequent page loads if the flag exists.

Categories
JavaScript Answers

How to toggle a checkbox inside of a div when clicking on the div using JavaScript?

Sometimes, we want to toggle a checkbox inside of a div when clicking on the div using JavaScript.

In this article, we’ll look at how to toggle a checkbox inside of a div when clicking on the div using JavaScript.

How to toggle a checkbox inside of a div when clicking on the div using JavaScript?

To toggle a checkbox inside of a div when clicking on the div using JavaScript, we can add a click handler to the div.

For instance, we write:

<div>
  <input type='checkbox'>
  toggle
</div>

to add a div with a checkbox input inside.

Then we write:

const div = document.querySelector('div')
div.onclick = (e) => {
  const input = e.target.querySelector('input')
  input.checked = !input.checked
}

to select the div with querySelector.

Next, we set div.onclick to a function that selects the input inside the div with e.target.querySelector.

e.target is the div.

Then we set the checked property to the negation of the current checked value to toggle the checkbox.

Conclusion

To toggle a checkbox inside of a div when clicking on the div using JavaScript, we can add a click handler to the div.

Categories
JavaScript Answers

How to split an array into two arrays based on odd/even position with JavaScript?

Sometimes, we want to split an array into two arrays based on odd/even position with JavaScript.

In this article, we’ll look at how to split an array into two arrays based on odd/even position with JavaScript.

How to split an array into two arrays based on odd/even position with JavaScript?

To split an array into two arrays based on odd/even position with JavaScript, we can use the array filter method.

For instance, we write:

const arr = [1, 1, 2, 2, 3, 8, 4, 6]
const aOdd = arr.filter((_, i) => i % 2 === 1)
const aEven = arr.filter((_, i) => i % 2 === 0)
console.log(aOdd)
console.log(aEven)

to call arr.filter with (_, i) => i % 2 === 1 to return all the arr elements with odd indexes.

Likewise, we call arr.filter with (_, i) => i % 2 === 0 to return all the arr elements with event indexes.

i is the index of the element in arr.

Therefore, aOdd is [1, 2, 8, 6] and aEven is [1, 2, 3, 4].

Conclusion

To split an array into two arrays based on odd/even position with JavaScript, we can use the array filter method.