Categories
JavaScript Answers

How to get the child node count with JavaScript?

Sometimes, we want to get the child node count with JavaScript.

In this article, we’ll look at how to get the child node count with JavaScript.

How to get the child node count with JavaScript?

To get the child node count with JavaScript, we can use the childElementCount property.

For instance, we write:

<ul>
  <li>Array1</li>
  <li>Array2</li>
  <li id="element">Array3</li>
</ul>

to add a ul with some li elements.

Then we write:

const temp = document.getElementById('element').parentNode;
console.log(temp.childElementCount);

We select the li with ID element with getElementById.

Then we get the ul with parentNode.

Finally, we get the number of li’s in the ul with childElementCount.

Therefore, the console log should log 3.

Conclusion

To get the child node count with JavaScript, we can use the childElementCount property.

Categories
JavaScript Answers

How to mount the child components only after data has been loaded with Vue.js and JavaScript?

Sometimes, we want to mount the child components only after data has been loaded with Vue.js and JavaScript.

In this article, we’ll look at how to mount the child components only after data has been loaded with Vue.js and JavaScript.

How to mount the child components only after data has been loaded with Vue.js and JavaScript?

To mount the child components only after data has been loaded with Vue.js and JavaScript, we can use the v-if directive to check if the data is loaded before rendering the child component.

For instance, we write:

<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>

<div id='app'>

</div>

to add the Vue script and app container.

Then we write:

const Foo = {
  template: `<p>{{index}}</p>`,
  props: ['index']
}

const v = new Vue({
  el: '#app',
  template: `
    <div v-if='arr'>
    	<foo :key='i' :index='i' v-for='i of arr' />
    </div>
  `,
  data: {
    arr: undefined
  },
  components: {
    Foo
  },
  mounted() {
    setTimeout(() => {
      this.arr = [1, 2, 3, 4, 5]
    }, 1000)
  }
})

to add v-if to the div to see if arr is defined before we use v-for to render the array entries.

In the mounted hook, we assign this.arr to an array in the setTimeout callback, so there’s a delay until arr is assigned to the array.

Therefore, we should see a delay before Foo is rendered.

Conclusion

To mount the child components only after data has been loaded with Vue.js, we can use the v-if directive to check if the data is loaded before rendering the child component.

Categories
JavaScript Answers

How to detect if JavaScript object is a FormData instance?

Sometimes, we want to detect if JavaScript object is a FormData instance.

In this article, we’ll look at how to detect if JavaScript object is a FormData instance.

How to detect if JavaScript object is a FormData instance?

To detect if JavaScript object is a FormData instance, we can use the instanceof operator.

For instance, we write:

const formData = new FormData()
console.log(formData instanceof FormData)

to create a new FormData instance and assign it to formData.

Next, we check if formData is an instance of FormData with the instanceof operator.

The console log should log true since formData is a FormData instance.

Conclusion

To detect if JavaScript object is a FormData instance, we can use the instanceof operator.

Categories
JavaScript Answers

How to use Promise.race() with JavaScript?

Sometimes, we want to use Promise.race() with JavaScript.

In this article, we’ll look at how to use Promise.race() with JavaScript.

How to use Promise.race() with JavaScript?

To use Promise.race() with JavaScript, we can call it with an array of promises.

For instance, we write:

const p1 = new Promise((resolve, reject) => {
  setTimeout(resolve, 500, 'one');
});
const p2 = new Promise((resolve, reject) => {
  setTimeout(resolve, 100, 'two');
});

(async () => {
  const value = await Promise.race([p1, p2])
  console.log(value);
})()

We call Promise.race with [p1, p2] which will return the promise that is settled first.

Therefore, value should be 'two' since the setTimeout callback is run in 100 seconds after the callback is queued for running.

Conclusion

To use Promise.race() with JavaScript, we can call it with an array of promises.

Categories
JavaScript Answers

How to reset a GIF animation with JavaScript?

Sometimes, we want to reset a GIF animation with JavaScript.

In this article, we’ll look at how to reset a GIF animation with JavaScript.

How to reset a GIF animation with JavaScript?

To reset a GIF animation with JavaScript, we can add a random query string to the end of the image URL.

For instance, we write:

<img src='https://media4.giphy.com/avatars/JulietGlock/S2TkRZ9GyF91.GIF' style='height: 100px'>

to add an animate GIF img element.

Then we write:

const img = document.querySelector('img')
setTimeout(() => {
  img.src = `${img.src.replace(/\?.*$/,"")}?x=${Math.random()}`;
}, 2000)

to select the img element with querySelector.

Then we set the img.src property to the img.src plus a new query string that had the x parameter set to a random number.

We have that in the setTimeout callback so src will be changed with a delay.

The animated GIF will start from the beginning when src is changed.

Conclusion

To reset a GIF animation with JavaScript, we can add a random query string to the end of the image URL.