Categories
JavaScript Answers

How to get contentEditable caret position with JavaScript?

Sometimes, we want to get contentEditable caret position with JavaScript.

In this article, we’ll look at how to get contentEditable caret position with JavaScript.

How to get contentEditable caret position with JavaScript?

To get contentEditable caret position with JavaScript, we change the selection to start from the beginning and then get the length of the full selected string.

For instance, we write

const cursorPosition = () => {
  const sel = document.getSelection();
  sel.modify("extend", "backward", "paragraphboundary");
  const pos = sel.toString().length;
  if (sel.anchorNode !== undefined) {
    sel.collapseToEnd();
  }

  return pos;
};

to get the selection with getSelection.

Then we move the selection back to the beginning of the input value with

sel.modify("extend", "backward", "paragraphboundary");

And then we get the caret position pos with the length of the selection.

Conclusion

To get contentEditable caret position with JavaScript, we change the selection to start from the beginning and then get the length of the full selected string.

Categories
JavaScript Answers

How to remove part of a string before a “:” in JavaScript?

Sometimes, we want to remove part of a string before a ":" in JavaScript.

In this article, we’ll look at how to remove part of a string before a ":" in JavaScript.

How to remove part of a string before a ":" in JavaScript?

To remove part of a string before a ":" in JavaScript, we call the string split method and the array pop method.

For instance, we write

const str = "abc: Lorem ipsum sit amet";
str = str.split(":").pop();

to call split with ':' to split the string by the colons.

And then we call pop to remove the part after the colon from the returned array and return it.

Conclusion

To remove part of a string before a ":" in JavaScript, we call the string split method and the array pop method.

Categories
JavaScript Answers

How to get city name using geolocation with JavaScript?

Sometimes, we want to get city name using geolocation with JavaScript.

In this article, we’ll look at how to get city name using geolocation with JavaScript.

How to get city name using geolocation with JavaScript?

To get city name using geolocation with JavaScript, we can use the geolocator.js library.

To install it, we add

<script type="text/javascript" src="geolocator.min.js"></script>

into our HTML code.

Then we write

const options = {
  enableHighAccuracy: true,
  fallbackToIP: true,
  addressLookup: true,
};

geolocator.locate(options, (err, location) => {
  console.log(location.address.city);
});

to call geolocator.locate with the options object to set some options.

We set fallbackToIP to true to fallback to using IP address for getting location if geolocation isn’t available.

And we set addressLookup to true to get the address data from the location.

Then we get the city name with location.address.city in the callback.

Conclusion

To get city name using geolocation with JavaScript, we can use the geolocator.js library.

Categories
JavaScript Answers

How to trim spaces from start and end of string with JavaScript?

Sometimes, we want to trim spaces from start and end of string with JavaScript.

In this article, we’ll look at how to trim spaces from start and end of string with JavaScript.

How to trim spaces from start and end of string with JavaScript?

To trim spaces from start and end of string with JavaScript, we use the string trim method.

For instance, we write

title = title.trim();

to call title.trim to return a string with the title string’s starting and ending whitespaces trimmed out.

Conclusion

To trim spaces from start and end of string with JavaScript, we use the string trim method.

Categories
JavaScript Answers

How to replace all whitespace characters with JavaScript?

Sometimes, we want to replace all whitespace characters with JavaScript.

In this article, we’ll look at how to replace all whitespace characters with JavaScript.

How to replace all whitespace characters with JavaScript?

To replace all whitespace characters with JavaScript, we can use the string replace method with a regex.

For instance, we write

str = str.replace(/\s/g, "X");

to call str.replace with /\s/g to match all whitespace characters.

\s matches all whitespace characters.

The g flag make replace get all matches.

And we replace them all with 'X'.

Conclusion

To replace all whitespace characters with JavaScript, we can use the string replace method with a regex.