Categories
JavaScript Answers

How to replace a window’s URL hash with another response with JavaScript?

Sometimes, we want to replace a window’s URL hash with another response with JavaScript.

In this article, we’ll look at how to replace a window’s URL hash with another response with JavaScript.

How to replace a window’s URL hash with another response with JavaScript?

To replace a window’s URL hash with another response with JavaScript, we can set the window.location.hash property.

For instance, we write

window.location.hash = "#food";

to replace the hash of the current URL with #food.

Conclusion

To replace a window’s URL hash with another response with JavaScript, we can set the window.location.hash property.

Categories
JavaScript Answers

How to set time with date in moment.js and JavaScript?

Sometimes, we want to set time with date in moment.js and JavaScript.

In this article, we’ll look at how to set time with date in moment.js and JavaScript.

How to set time with date in moment.js and JavaScript?

To set time with date in moment.js and JavaScript, we use the set method.

For instance, we write

const hours = 15;
const minutes = 32;
const date = moment("2022-05-21").set("hour", hours).set("minute", minutes);

to call “setwith thehoursandminutes` to set the hours and minutes.

The first argument is the unit and the 2nd argument is the value to set.

A new moment object is returned with the new time value.

Conclusion

To set time with date in moment.js and JavaScript, we use the set method.

Categories
JavaScript Answers

How to get the second to last item of an array with JavaScript?

Sometimes, we want to get the second to last item of an array with JavaScript.

In this article, we’ll look at how to get the second to last item of an array with JavaScript.

How to get the second to last item of an array with JavaScript?

To get the second to last item of an array with JavaScript, we use the array at method.

For instance, we write

const arr = [1, 2, 3, 4];
const secondLast = arr.at(-2);

to call arr.at with -2 to return the 2nd last item of the arr array.

Therefore, secondLast is 3.

Conclusion

To get the second to last item of an array with JavaScript, we use the array at method.

Categories
HTML

How to create a select box with a search option with HTML?

Sometimes, we want to create a select box with a search option with HTML.

In this article, we’ll look at how to create a select box with a search option with HTML.

How to create a select box with a search option with HTML?

To create a select box with a search option with HTML, we use the datalist element.

For instance, we write

<input list="brow" />
<datalist id="brow">
  <option value="Internet Explorer"></option>
  <option value="Firefox"></option>
  <option value="Chrome"></option>
  <option value="Opera"></option>
  <option value="Safari"></option>
</datalist>

to add a datalist element with an id that matches the list attribute value to use the option elements as the autocomplete choices.

When we type in the input, we should see the possible matches show in a drop down list.

Conclusion

To create a select box with a search option with HTML, we use the datalist element.

Categories
JavaScript Answers

How to split string with white space with JavaScript?

Sometimes, we want to split string with white space with JavaScript.

In this article, we’ll look at how to split string with white space with JavaScript.

How to split string with white space with JavaScript?

To split string with white space with JavaScript, we use the string split method.

For instance, we write

const arr = w.split(/(\s+)/).filter((e) => e.trim().length > 0);

to call split with a regex that matches whitespaces.

Then we call filter with a function that returns the strings that has length bigger than 0 after trimming the starting and ending whitespaces with trim.

/(\s+)/ matches whitespaces in string w and use them as separators.

Conclusion

To split string with white space with JavaScript, we use the string split method.