Categories
JavaScript Answers Vue Vue Answers

How to invert boolean inline on click with Vue.js and JavaScript?

Sometimes, we want to invert boolean inline on click with Vue.js and JavaScript.

In this article, we’ll look at how to invert boolean inline on click with Vue.js and JavaScript.

How to invert boolean inline on click with Vue.js and JavaScript?

To invert boolean inline on click with Vue.js and JavaScript, we can set the @click directive to a statement to toggle the boolean.

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 v = new Vue({
  el: '#app',
  template: `
    <div>
      <button @click='toggle = !toggle'>
        {{ toggle ? 'show' : 'hide' }}
      </button>
    </div>
  `,
  data: {
    toggle: true
  },
})

to set @click to toggle = !toggle to toggle the toggle reactive property.

And we display show' if toggleistrue` and ‘hide’ otherwise.

Therefore, when we click the button, we see the button text switch between ‘show’ and ‘hide’.

Conclusion

To invert boolean inline on click with Vue.js and JavaScript, we can set the @click directive to a statement to toggle the boolean.

Categories
JavaScript Answers

How to select multiple options with JavaScript?

Sometimes, we want to select multiple options with JavaScript.

In this article, we’ll look at how to select multiple options with JavaScript.

How to select multiple options with JavaScript?

To select multiple options with JavaScript, we can set the selected property of the option we want to select to true.

For instance, we write:

<select multiple>
  <option value="1">One</option>
  <option value="2">two</option>
  <option value="3">three</option>
</select>

to add a multi-select element.

Then we write:

const select = document.querySelector('select')
for (const o of select.options) {
  if (+o.value < 3) {
    o.selected = true
  }
}

to select the select element with querySelector.

Then we loop through the options in the element with the for-of loop.

We get the options with select.options.

And if o.value is less than 3, we set o.selected to true.

Therefore, the first 2 choices are selected since their value attributes have value less than 3 when converted to a number.

Conclusion

To select multiple options with JavaScript, we can set the selected property of the option we want to select to true.

Categories
JavaScript Answers

How to hide a div when it loses focus with JavaScript?

Sometimes, we want to hide a div when it loses focus with JavaScript.

In this article, we’ll look at how to hide a div when it loses focus with JavaScript.

How to hide a div when it loses focus with JavaScript?

To hide a div when it loses focus with JavaScript, we can listen to the mouseup event on the document.

And if we lift our mouse button anywhere outside the div, then we hide the div.

For instance, we write:

<div>
  hello world
</div>

to add a div.

Then we write:

const div = document.querySelector('div')
document.onmouseup = (e) => {
  if (e.target !== div) {
    div.style.display = 'none'
  }
}

to select a div.

Then we set document.onmouseup to a function that checks if the element being clicked isn’t a div with e.target !== div.

e.target is the element where the event is being triggered.

If it’s true, then we set the display CSS property of the div to 'none' to hide it.

Conclusion

To hide a div when it loses focus with JavaScript, we can listen to the mouseup event on the document.

And if we lift our mouse button anywhere outside the div, then we hide the div.

Categories
JavaScript Answers

How to get element By using class name with JavaScript?

Sometimes, we want to get element By using class name with JavaScript.

In this article, we’ll look at how to get element By using class name with JavaScript.

How to get element By using class name with JavaScript?

To get element By using class name with JavaScript, we can use the getElementsByClassName method.

For instance, we write:

<div class='foo'>

</div>
<div class='foo'>

</div>
<div class='foo'>

</div>

to add 3 divs with class foo.

Then we write:

const els = document.getElementsByClassName('foo')
console.log(els)

to call getElementsByClassName with 'foo' to return all the elements with class name foo in an HTMLCollection object.

Conclusion

To get element By using class name with JavaScript, we can use the getElementsByClassName method.

Categories
JavaScript Answers

How to set date always to eastern time regardless of user’s time zone with JavaScript?

To set date always to eastern time regardless of user’s time zone with JavaScript, we can use the moment-timezone library.

To use it, we write:

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.34/moment-timezone.min.js" ></script>

to add the moment.js and moment-timezone scripts.

Then we write:

moment.tz.add([
  'America/New_York|EST EDT|50 40|0101|1Lz50 1zb0 Op0'
]);

const d = new Date(2022, 5, 1);
const myTimezone = "America/New_York";
const myDatetimeFormat = "YYYY-MM-DD hh:mm:ss a z";
const myDatetimeString = moment(d).tz(myTimezone).format(myDatetimeFormat);
console.log(myDatetimeString)

to call moment.tz.add to add the time zone data for Eastern time.

Then we create a date d with the Date constructor.

Next, we call moment with d to create a moment object.

Then we call tz with myTimezone to convert the time to the given time zone.

Next, we call format to return a date string with the format specified by myDatetimeFormat.

Therefore, myDatetimeString is '2022-06-01 03:00:00 am EDT'.