Categories
JavaScript Answers

How to use JavaScript to sort contents of select element?

Sometimes, we want to use JavaScript to sort contents of select element.

In this article, we’ll look at how to use JavaScript to sort contents of select element.

How to use JavaScript to sort contents of select element?

To use JavaScript to sort contents of select element, we use the sort method.

For instance, we write

const optionNodes = Array.from(selectNode.children);
const comparator = new Intl.Collator(lang.slice(0, 2)).compare;

const sorted = optionNodes.sort((a, b) =>
  comparator(a.textContent, b.textContent)
);

to call Array.from to convert the node list with select element’s child elements into an array.

Then we get the compare function from the Intl.Collator constructor.

And then we call sort with a callback that calls comparator to return an array sorted by the child element’s text content.

Conclusion

To use JavaScript to sort contents of select element, we use the sort method.

Categories
JavaScript Answers

How to get the current date in DD-Mon-YYYY format in JavaScript?

Sometimes, we want to get the current date in DD-Mon-YYY format in JavaScript.

In this article, we’ll look at how to get the current date in DD-Mon-YYY format in JavaScript.

How to get the current date in DD-Mon-YYY format in JavaScript?

To get the current date in DD-Mon-YYY format in JavaScript, we call the format method.

For instance, we write

const dateStr = moment().format("DD-MMM-YYYY");

to call moment to return a moment object with the current date time.

And then we call format to return a string in the DD-Mon-YYYY format.

Conclusion

To get the current date in DD-Mon-YYY format in JavaScript, we call the format method.

Categories
JavaScript Answers

How to find last index of element inside array by certain condition with JavaScript?

Sometimes, we want to find last index of element inside array by certain condition with JavaScript.

In this article, we’ll look at how to find last index of element inside array by certain condition with JavaScript.

How to find last index of element inside array by certain condition with JavaScript?

To find last index of element inside array by certain condition with JavaScript, we use the map and lastIndexOf methods.

For instance, we write

const lastIndex = elements.map((e) => e.a).lastIndexOf("something");

to call map and lastIndex to return an array with the a property value of each item in elements.

And then we call lastIndexOf to look for the last index of 'something' in the returned array.

Conclusion

To find last index of element inside array by certain condition with JavaScript, we use the map and lastIndexOf methods.

Categories
JavaScript Answers

How to define a JavaScript regex for special characters?

Sometimes, we want to define a JavaScript regex for special characters.

In tis article, we’ll look at how to define a JavaScript regex for special characters.

How to define a JavaScript regex for special characters?

To define a JavaScript regex for special characters, we use the \W pattern.

For instance, we write

const regex = /\W|_/g;

to use the \W pattern to match all non-word characters.

Conclusion

To define a JavaScript regex for special characters, we use the \W pattern.

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.