Categories
JavaScript Answers

How to detect back button or hash change in URL with JavaScript?

Sometimes, we want to detect back button or hash change in URL with JavaScript.

In this article, we’ll look at how to detect back button or hash change in URL with JavaScript.

How to detect back button or hash change in URL with JavaScript?

To detect back button or hash change in URL with JavaScript, we listen to the popstate event.

For instance, we write

window.addEventListener("popstate", (event) => {
  console.log(document.location, event.state);
});

to call addEventListener to listen to the popstate event triggered by window.

In the event handler callback, we get the current location with document.location.

And we get the change made with event.state.

Conclusion

To detect back button or hash change in URL with JavaScript, we listen to the popstate event.

Categories
JavaScript Answers

How to get array elements fulfilling a condition with JavaScript?

Sometimes, we want to get array elements fulfilling a condition with JavaScript.

In this article, we’ll look at how to get array elements fulfilling a condition with JavaScript.

How to get array elements fulfilling a condition with JavaScript?

To get array elements fulfilling a condition with JavaScript, we use the array filter method.

For instance, we write

const filterResult = [1, 16, 4, 6].filter((x) => {
  return x > 5;
});
console.log(filterResult);

to call [1, 16, 4, 6].filter with a function that checks if the item x being iterated through is bigger than 5.

If it’s bigger than 5, then it’ll be included in the returned array.

Conclusion

To get array elements fulfilling a condition with JavaScript, we use the array filter method.

Categories
JavaScript Answers

How to disable an option in a select based on its value with JavaScript?

Sometimes, we want to disable an option in a select based on its value with JavaScript.

In this article, we’ll look at how to disable an option in a select based on its value with JavaScript.

How to disable an option in a select based on its value with JavaScript?

To disable an option in a select based on its value with JavaScript, we can set its disabled property to true.

For instance, we write

document.querySelectorAll("#foo option").forEach((opt) => {
  if (opt.value === "example") {
    opt.disabled = true;
  }
});

to select the option elements in the drop down with querySelector.

Then we call forEach with a callback that checks if the value attribute’s value is 'example'.

If it is, then we set its disabled property to true to disable it.

Conclusion

To disable an option in a select based on its value with JavaScript, we can set its disabled property to true.

Categories
JavaScript Answers

How to change audio element src with JavaScript?

Sometimes, we want to change audio element src with JavaScript.

In this article, we’ll look at how to change audio element src with JavaScript.

How to change audio element src with JavaScript?

To change audio element src with JavaScript, we set the element’s src attribute.

For instance, we write

const audio = document.getElementById("audio");

const source = document.getElementById("audioSource");
source.src = elm.getAttribute("data-value");

audio.load();
audio.play();

to select the audio element with getElementById.

Then we get the element with the data-value attribute with getElementById.

We use the data-value attribute value for the src attribute’s value.

Next we call load to preload the audio element with the clip from the new URL.

Then we call play to play it.

Conclusion

To change audio element src with JavaScript, we set the element’s src attribute.

Categories
Vue Answers

How to set the selected option in Vue.js 2?

Sometimes, we want to the set selected in Vue.js 2.

In this article, we’ll look at how to the set selected in Vue.js 2.

How to set the selected option in Vue.js 2?

To set selected option selected in Vue.js 2, we set the v-model variable to the selected option’s value attribute value.

For instance, we write

<template>
  <select v-model="selectedDay">
    <option v-for="day in days">{{ day }}</option>
  </select>
</template>

<script>
export default {
  data() {
    return {
      selectedDay: 1,
      days: Array.from({ length: 31 }, (v, i) => i).slice(1),
    };
  },
  mounted() {
    const selectedDay = new Date();
    this.selectedDay = selectedDay.getDate();
  },
};
</script>

to add the select drop down in the template.

We render the days values as options with v-for.

Then we define the days array in the data method.

Next, we set this.selectedDay to today’s date that we get from calling getDate on the current date which is selectedDay.

Conclusion

To set selected option selected in Vue.js 2, we set the v-model variable to the selected option’s value attribute value.