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.

Categories
JavaScript Answers

How to select nested element with class with JavaScript?

Sometimes, we want to select nested element with class with JavaScript.

In this article, we’ll look at how to select nested element with class with JavaScript.

How to select nested element with class with JavaScript?

To select nested element with class with JavaScript, we can use the querySelector method.

For instance, we write:

<div class="cl1">
  <h1>Some heading</h1>
  <div class="sl-desc">Some description</div>
  <div class="sl-price">from only $10</div>
</div>

to add a div with nested divs.

Then we can select the div with class s1-price in the div with class cl1 by writing:

const el = document.querySelector(".cl1 .sl-price")
console.log(el.textContent)

We call document.querySelector with ".cl1 .sl-price" to do the selection.

And then we use the textContent property to get its text content.

Therefore, the console log logs "from only $10".

Conclusion

To select nested element with class with JavaScript, we can use the querySelector method.

Categories
JavaScript Answers

How to make a promise wait a couple of seconds before return with JavaScript?

Sometimes, we want to make a promise wait a couple of seconds before return with JavaScript.

In this article, we’ll look at how to make a promise wait a couple of seconds before return with JavaScript.

How to make a promise wait a couple of seconds before return with JavaScript?

To make a promise wait a couple of seconds before return with JavaScript, we can create a promise that pauses the code for a few seconds.

For instance, we write:

const wait = (milliseconds) => {
  return new Promise(resolve => setTimeout(resolve, milliseconds));
}

(async () => {
  console.log(1)
  await wait(2000)
  console.log(2)
})()

to define the wait function that calls setTimeout with resolve and the number of milliseconds to wait until resolve is called.

Then we can use it to pause our code before running the next line by calling wait with the delay in milliseconds.

Therefore, 2 is logged 2 seconds after 1 is logged.

Conclusion

To make a promise wait a couple of seconds before return with JavaScript, we can create a promise that pauses the code for a few seconds.