Categories
JavaScript Answers

How to wait for a click event to complete with JavaScript?

Sometimes, we want to wait for a click event to complete with JavaScript.

In this article, we’ll look at how to wait for a click event to complete with JavaScript.

How to wait for a click event to complete with JavaScript?

To wait for a click event to complete with JavaScript, we can add a click listener function.

For instance, we write:

<div>
  hello world
</div>

to add a div.

Then we write:

const div = document.querySelector('div');
div.onclick = () => {
  console.log('clicked')
}

to select the div with querySelector.

Next, we set div.onclick to a function that logs 'clicked'.

Now when the click event is emitted, we see 'clicked' logged.

Conclusion

To wait for a click event to complete with JavaScript, we can add a click listener function.

Categories
JavaScript Answers

How to read individual transform style values in JavaScript?

Sometimes, we want to read individual transform style values in JavaScript.

In this article, we’ll look at how to read individual transform style values in JavaScript.

How to read individual transform style values in JavaScript?

To read individual transform style values in JavaScript, we can use some string and array methods.

For instance, we write:

<div style="transform: translate(10px, 20px)">
  hello world
</div>

to add a div.

Then we write:

const element = document.querySelector('div');
const styles = window.getComputedStyle(element, null);
const matrixPattern = /^\w*\((((\d+)|(\d*\.\d+)),\s*)*((\d+)|(\d*\.\d+))\)/i;
const vals = [...styles.transform.match(matrixPattern)].slice(1).filter(Boolean).map(v => +v.replace(/\D+/, ''))
console.log(vals)

to select the div with querySelector.

Then we get the styles with getComputedStyle.

Next, we call styles.transform.match with matrixPattern to get all the numbers from the CSS style value.

Then we spread the values into an array.

Next, we call slice to remove the match with all the number values in one string.

Then we call filter to remove the falsy values.

Finally, we call map with a callback to remove all non-digit characters and use + to convert the string to number.

As a result, vals is [10, 10, 10, 20, 20].

Conclusion

To read individual transform style values in JavaScript, we can use some string and array methods.

Categories
JavaScript Answers

How to stop propagation of mousedown or mouseup from a click handler with JavaScript?

Sometimes, we want to stop propagation of mousedown or mouseup from a click handler with JavaScript.

In this article, we’ll look at how to stop propagation of mousedown or mouseup from a click handler with JavaScript.

How to stop propagation of mousedown or mouseup from a click handler with JavaScript?

To stop propagation of mousedown or mouseup from a click handler with JavaScript, we can call stopPropagation.

For instance, we write:

<div id="outer">
  <div id="inner">
    hello world
  </div>
</div>

to add a inner div.

Then we write:

const inner = document.getElementById('inner')
inner.onmouseup = (e) => {
  e.stopPropagation()
}
inner.onmousedown = (e) => {
  e.stopPropagation()
}

to select the inner div with getElementById.

Then we set the onmouseup and onmousedown properties to a function that calls stopPropagation to stop propagation of the mouseup and mousedown events triggered by the div.

Conclusion

To stop propagation of mousedown or mouseup from a click handler with JavaScript, we can call stopPropagation.

Categories
JavaScript Answers

How to fix setting innerHTML not updating the DOM with JavaScript?

Sometimes, we want to fix setting innerHTML not updating the DOM with JavaScript.

In this article, we’ll look at how to fix setting innerHTML not updating the DOM with JavaScript.

How to fix setting innerHTML not updating the DOM with JavaScript?

To fix setting innerHTML not updating the DOM with JavaScript, we should set the innerHTML property of the element to an HTML string.

For instance, we write:

<div>

</div>

to add a div.

Then we write:

const div = document.querySelector('div')
div.innerHTML = '<b>foo</b>'

to select the div with querySelector.

Then we set div.innerHTML to a string with the elements we want to put in the div.

Conclusion

To fix setting innerHTML not updating the DOM with JavaScript, we should set the innerHTML property of the element to an HTML string.

Categories
JavaScript Answers Vue Vue Answers

How to submit form in Vue.js with Ajax and JavaScript?

Sometimes, we want to submit form in Vue.js with Ajax and JavaScript.

In this article, we’ll look at how to submit form in Vue.js with Ajax and JavaScript.

How to submit form in Vue.js with Ajax and JavaScript?

To submit form in Vue.js with Ajax and JavaScript, we can create a FormData instance from the form values.

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 the app container.

Then we write:

const v = new Vue({
  el: '#app',
  template: `
    <div>
      <form @submit.prevent='submit'>
        <input name='title'>
        <input name='content'>
        <input type='submit'>
      </form>
    </div>
  `,
  methods: {
    async submit(e) {
      fetch('https://jsonplaceholder.typicode.com/posts', {
        method: 'POST',
        body: new FormData(e.target),
      })
    }
  }
})

to add a form.

We set the @submit.prevent directive to submit run submit when we click Submit.

In submit, we create a FormData instance by passing e.target into it, where e.target is the form element.

As a result, when we click Submit, the form values with the name attribute values as the keys and the input values as their corresponding values will be submitted.

Conclusion

To submit form in Vue.js with Ajax and JavaScript, we can create a FormData instance from the form values.