Categories
Vue Answers

How to concatenate variable and text with Vue.js img src?

Sometimes, we want to concatenate variable and text with Vue.js img src.

In this article, we’ll look at how to concatenate variable and text with Vue.js img src.

How to concatenate variable and text with Vue.js img src?

To concatenate variable and text with Vue.js img src, we can do the concatenation in the src prop.

For instance, we write

<template>
  <img :src="imgPreUrl + 'img/logo.png'" />
</template>

to set the src prop of the img element to the imgPreUrl + 'img/logo.png', which concatenates imgPreUrl and 'img/logo.png' together.

Conclusion

To concatenate variable and text with Vue.js img src, we can do the concatenation in the src prop.

Categories
JavaScript Answers

How to differentiate single click event and double click event with JavaScript?

Sometimes, we want to differentiate single click event and double click event with JavaScript.

In this article, we’ll look at how to differentiate single click event and double click event with JavaScript.

How to differentiate single click event and double click event with JavaScript?

To differentiate single click event and double click event with JavaScript, we can use the detail property.

For instance, we write

element.onclick = (event) => {
  if (event.detail === 1) {
    // ...
  } else if (event.detail === 2) {
    // ...
  }
};

to add an click listener to element.

In the onclick method, we check if details is 1 or 2.

If it’s 1, then it’s a single click.

If it’s 2, then it’s a double click.

Conclusion

To differentiate single click event and double click event with JavaScript, we can use the detail property.

Categories
JavaScript Answers

How to set selected value of jQuery Select2 with JavaScript?

To set selected value of jQuery Select2 with JavaScript, we call val and then trigger.

For instance, we write

$("#drpServices").select2().val("0").trigger("change");

to create the drop down with

$("#drpServices").select2()

Then we set its value with the val method to the option with value attribute set to 0.

Finally, we call trigger with 'change' to update the drop down to show the value we set.

Categories
JavaScript Answers

How to set date in input type date with JavaScript?

Sometimes, we want to set date in input type date with JavaScript.

In this article, we’ll look at how to set date in input type date with JavaScript.

How to set date in input type date with JavaScript?

To set date in input type date with JavaScript, we can set the valueAsDate property of the date input.

For instance, we write

document.getElementById("datePicker").valueAsDate = new Date();

to select the date input with getElementById.

Then we set its valueAsDate property to a date object with the current date time to set the date input’s value.

Conclusion

To set date in input type date with JavaScript, we can set the valueAsDate property of the date input.

Categories
JavaScript Answers

How to check if date between dates with Moment.js and JavaScript?

Sometimes, we want to check if date between dates with Moment.js and JavaScript.

In this article, we’ll look at how to check if date between dates with Moment.js and JavaScript.

How to check if date between dates with Moment.js and JavaScript?

To check if date between dates with Moment.js and JavaScript, we can use the isBetween method.

For instance, we write

const compareDate = moment("15/02/2022", "DD/MM/YYYY");
const startDate = moment("12/01/2022", "DD/MM/YYYY");
const endDate = moment("15/01/2022", "DD/MM/YYYY");
const isBetween = compareDate.isBetween(startDate, endDate, "days", "()");

to call compareDate.isBetween with startDate and endDate to check is compareDate is between startDate and endDate when comparing days.

The last argument is the inclusivity value, which specifies we compare exclusively.

We can also use '(]' for right inclusive, '[)' for left inclusive, or '[]' for all inclusive.

Conclusion

To check if date between dates with Moment.js and JavaScript, we can use the isBetween method.