Categories
JavaScript Answers

How to disable or enable submit button until all forms have been filled with JavaScript?

Sometimes, we want to disable or enable submit button until all forms have been filled with JavaScript.

In this article, we’ll look at how to disable or enable submit button until all forms have been filled with JavaScript.

How to disable or enable submit button until all forms have been filled with JavaScript?

To disable or enable submit button until all forms have been filled with JavaScript, we can set the disabled property according to the input values.

For instance, we write:

<form name="theForm">
  <input type="text" />
  <input type="text"  />
  <input id="submitbutton" type="submit" disabled />
</form>

We have a form that has 2 text inputs and a submit button.

Then to enable the submit button only when all the text inputs are filled, we write:

document.onkeyup = (e) => {
  if (e.target.tagName === 'INPUT') {
    const canSubmit = [...document.forms.theForm.querySelectorAll('input[type="text"]')]
      .every(i => {
        return i.value
      })
    document.forms.theForm.querySelector('input[type="submit"]').disabled = !canSubmit
  }
}

We set the document.onkeyup function to a function that checks if the keyup event is triggered by an input element.

If it is, then we check if all the text inputs are filled with:

const canSubmit = [...document.forms.theForm.querySelectorAll('input[type="text"]')]
  .every(i => {
    return i.value
  })

Then we set the disabled attribute of the submit button with:

document.forms.theForm.querySelector('input[type="submit"]').disabled = !canSubmit

Now the submit button is only enabled when all the text inputs are filled.

Conclusion

To disable or enable submit button until all forms have been filled with JavaScript, we can set the disabled property according to the input values.

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?

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

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

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

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.

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 create a canvas 2d context without displaying the canvas with JavaScript?

Sometimes, we want to create a canvas 2d context without displaying the canvas with JavaScript.

In this article, we’ll look at how to create a canvas 2d context without displaying the canvas with JavaScript.

How to create a canvas 2d context without displaying the canvas with JavaScript?

To create a canvas 2d context without displaying the canvas with JavaScript, we can use the document.createElement method.

For instance, we write:

const canvas = document.createElement('canvas');
canvas.width = 200;
canvas.height = 100;

const ctx = canvas.getContext('2d');
ctx.fillStyle = '#f00';
ctx.fillRect(20, 10, 80, 50);

const imgUrl = canvas.toDataURL();
const img = document.createElement('img');
img.src = imgUrl

document.body.appendChild(img)

to create the canvas with createElement.

Then we get the context with getContext.

Next, we set the fill style and add a rectangle with fillStyle and fillRect.

Then we convert the canvas to an image URL string with toDataURL.

Finally, we create an img element with createElement, set the src attribute to imgUrl and append the img element as a child of the body element with document.body.appendChild.

Now we should see a red rectangle displayed,

Conclusion

To create a canvas 2d context without displaying the canvas with JavaScript, we can use the document.createElement method.