Categories
Vue Answers

How to set static image src in Vue.js template with JavaScript?

Sometimes, we want to set static image src in Vue.js template with JavaScript.

In this article, we’ll look at how to set static image src in Vue.js template with JavaScript.

How to set static image src in Vue.js template with JavaScript?

To set static image src in Vue.js template with JavaScript, we set the path relative to the src folder.

For instance, we write

<template>
  <img src="/static/img/clear.gif" />
</template>

to add an img element with src set to a path relative to the src folder to load the image.

Conclusion

To set static image src in Vue.js template with JavaScript, we set the path relative to the src folder.

Categories
JavaScript Answers

How to count text area characters with JavaScript?

To count text area characters with JavaScript, we use the value property.

For instance, we write

document.getElementById("textarea").onkeyup = (e) => {
  console.log(e.target.value.length);
};

to select the text area with getElementById.

And then we set its onkeyup property to a function that logs the number of characters entered into the text area.

e.target.value has the input value string.

length has the number of characters.

Categories
JavaScript Answers

How to find, modify, and save with Mongoose and JavaScript?

To find, modify, and save with Mongoose and JavaScript, we call findOne and save.

For instance, we write

User.findOne({ username: oldUsername }, (err, user) => {
  user.username = newUser.username;
  user.password = newUser.password;
  user.rights = newUser.rights;

  user.save((err) => {
    if (err) {
      console.error("ERROR!");
    }
  });
});

to call findOne to find the object with the username set to oldUsername.

In the callback, we set the property values for user.

And then we call user.save to save the values.

Categories
JavaScript Answers

How to add 2 weeks to a JavaScript date?

Sometimes, we want to add 2 weeks to a JavaScript date.

In this article, we’ll look at how to add 2 weeks to a JavaScript date.

How to add 2 weeks to a JavaScript date?

To add 2 weeks to a JavaScript date, we use the Date constructor.

For instance, we write

const date = new Date();
date.setDate(date.getDate() + 14);

to create a date object.

Then we call getDate to get the current date of the month.

Next, we call setDate with the current date of the month plus 14 to set the date to the date of the month 14 days from today.

Conclusion

To add 2 weeks to a JavaScript date, we use the Date constructor.

Categories
JavaScript Answers

How to calculate date from today date to 7 days before with JavaScript?

Sometimes, we want to calculate date from today date to 7 days before with JavaScript.

In this article, we’ll look at how to calculate date from today date to 7 days before with JavaScript.

How to calculate date from today date to 7 days before with JavaScript?

To calculate date from today date to 7 days before with JavaScript, we use the Date constructor.

For instance, we write

const days = 7;
const date = new Date();
const last = new Date(date.getTime() - days * 24 * 60 * 60 * 1000);

to create a new date object.

And then we call getTime to get its timestamp in milliseconds.

Then we subtract that by 7 days after converting it to milliseconds.

Finally we put that in the Date constructor to create a new date 7 days before today.

Conclusion

To calculate date from today date to 7 days before with JavaScript, we use the Date constructor.