Categories
JavaScript Answers

How to remove the first character of a class name with JavaScript?

Sometimes, we want to remove the first character of a class name with JavaScript.

In this article, we’ll look at how to remove the first character of a class name with JavaScript.

How to remove the first character of a class name with JavaScript

To remove the first character of a class name with JavaScript, we can use the string’s substring method.

For instance, we write:

<div class='foo'>

</div>

to add a div with a class attribute set.

Then we write:

const div = document.querySelector('div')
const trimmed = div.className.substring(1);
console.log(trimmed)

to select the div with querySelector.

Then we get the class name with className.

And then we call substring with 1 to return the class name string without the first character.

Therefore trimmed is 'oo'.

Conclusion

To remove the first character of a class name with JavaScript, we can use the string’s substring method.

Categories
JavaScript Answers

How to generate short unique alphanumeric with Node.js and JavaScript?

Sometimes, we want to generate short unique alphanumeric with Node.js and JavaScript.

In this article, we’ll look at how to generate short unique alphanumeric with Node.js and JavaScript.

How to generate short unique alphanumeric with Node.js and JavaScript?

To generate short unique alphanumeric with Node.js and JavaScript, we can use the number’s toString method with base 36.

For instance, we write:

const s = (100000000000).toString(36);
console.log(s)

to call toString on 100000000000 with 36 to return a string with letters and digits.

As a result, s is '19xtf1ts'.

Conclusion

To generate short unique alphanumeric with Node.js and JavaScript, we can use the number’s toString method with base 36.

Categories
JavaScript Answers

How to extract anchor text and URL from anchor tags with JavaScript?

Sometimes, we want to extract anchor text and URL from anchor tags with JavaScript

In this article, we’ll look at how to extract anchor text and URL from anchor tags with JavaScript.

How to extract anchor text and URL from anchor tags with JavaScript?

To extract anchor text and URL from anchor tags with JavaScript, we can set the string as the innerHTML of an element.

For instance, we write:

const str = `
  <a href="http://yahoo.com">Yahoo</a>
  <a href="http://google.com">Google</a>
`
const div = document.createElement('div')
div.innerHTML = str
const anchors = div.querySelectorAll('a')
const arr = [...anchors].map((a) => {
  const {
    textContent,
    href
  } = a
  return {
    textContent,
    href
  }
})
console.log(arr)

to create a div, set the innerHTML of the div to the str` string, and then get the values by selecting the anchors.

To do this, we call createElement to creatr the div.

Then we set div.innerHTML to str to populate the div with the anchors.

Next, we select the anchors with div.querySelectorAll.

And then we spread the anchors into an array and call map to return the properties we’re looking for.

textContent has the anchor text and href has the link’s URL.

Therefore, arr is

[{
  href: "http://yahoo.com/",
  textContent: "Yahoo"
}, {
  href: "http://google.com/",
  textContent: "Google"
}]

Conclusion

To extract anchor text and URL from anchor tags with JavaScript, we can set the string as the innerHTML of an element.

Categories
JavaScript Answers

How to check for broken images in React with JavaScript?

Sometimes, we want to check for broken images in React with JavaScript

In this article, we’ll look at how to check for broken images in React with JavaScript.

How to check for broken images in React with JavaScript?

To check for broken images in React with JavaScript, we can set the onError of the img element to a error handler function.

For instance, we write:

import React from "react";

export default function App() {
  const onError = (e) => {
    console.log(e);
  };

  return (
    <>
      <img onError={onError} src="abc" alt="abc" />
    </>
  );
}

to set the onError of the img element to the onError function.

Since the src prop is set to an invalid URL, we see the error object logged in onError.

Conclusion

To check for broken images in React with JavaScript, we can set the onError of the img element to a error handler function.

Categories
JavaScript Answers

How to get last day of the month with JavaScript?

Sometimes, we want to get last day of the month with JavaScript.

In this article, we’ll look at how to get last day of the month with JavaScript.

How to get last day of the month with JavaScript?

To get last day of the month with JavaScript, we can use the Date constructor.

For instance, we write:

const lastDayOfMonth = (year, month) => {
  return new Date((new Date(year, month, 1)) - 1);
}

console.log(lastDayOfMonth(2022, 11))

to create a Date with the last day of the month as its value.

In it, we create a Date instance with the date of the first day of the next month with (new Date(year, month, 1).

Then we subtract that by 1 to return the timestamp of the last minute of the last day of the last month.

And we use the returned timestamp to create a new Date instance.

Therefore, the console log is Wed Nov 30 2022 23:59:59 GMT-0800 (Pacific Standard Time).

Conclusion

To get last day of the month with JavaScript, we can use the Date constructor.