Categories
JavaScript Answers

How to convert a JavaScript string variable to decimal or money?

Sometimes, we want to convert a JavaScript string variable to decimal or money.

In this article, we’ll look at how to convert a JavaScript string variable to decimal or money.

How to convert a JavaScript string variable to decimal or money?

To convert a JavaScript string variable to decimal or money, we can use the Intl.NumberFormat constructor.

For instance, we write

const formatter = new Intl.NumberFormat("en", {
  style: "currency",
  currency: "GBP",
});

console.log(formatter.format(1234.5));

to create the Intl.NumberFormat object by calling it with the locale string and an object with the formatting options.

'currency' formats the string as a currency number.

And currency is set to 'GBP' so the number is formatted into a GBP value.

Then we call formatter.format with the number to format to return a string with the currency number.

Conclusion

To convert a JavaScript string variable to decimal or money, we can use the Intl.NumberFormat constructor.

Categories
React Answers

How to toggle class on click with React?

Sometimes, we want to toggle class on click with React.

In this article, we’ll look at how to toggle class on click with React.

How to toggle class on click with React?

To toggle class on click with React, we can set the className prop.

For instance, we write

function MyComponent(props) {
  const [isActive, setActive] = useState(false);

  const toggleClass = () => {
    setActive(!isActive);
  };

  return (
    <div className={isActive ? "your-class" : null} onClick={toggleClass}>
      <p>{props.text}</p>
    </div>
  );
}

to create the MyComponent component.

In it, we have the isActive state.

We set it with the setActive function in the toggleClass function.

In the div, we set the className prop to 'your-class' if isActive is true and null otherwise.

And we set onClick to toggleClass so toggleClass is called when we click on the button.

Conclusion

To toggle class on click with React, we can set the className prop.

Categories
JavaScript Answers

How to convert date to UTC using moment.js and JavaScript?

Sometimes, we want to convert date to UTC using moment.js and JavaScript.

In this article, we’ll look at how to convert date to UTC using moment.js and JavaScript.

How to convert date to UTC using moment.js and JavaScript?

To convert date to UTC using moment.js and JavaScript, we can use the moment’s utc method.

For instance, we write

const utcStart = new moment("2022-06-24T09:00", "YYYY-MM-DDTHH:mm").utc();

to create a moment object with the datetime with moment.

Then we call utc to return a moment object with the same date in UTC.

Conclusion

To convert date to UTC using moment.js and JavaScript, we can use the moment’s utc method.

Categories
JavaScript Answers

How to mark a field invalid from JavaScript?

Sometimes, we want to mark a field invalid from JavaScript.

In this article, we’ll look at how to mark a field invalid from JavaScript.

How to mark a field invalid from JavaScript?

To mark a field invalid from JavaScript, we use the field element’s setCustomValidity method.

For instance, we write

field.setCustomValidity("Invalid field.");

to call field.setCustomValidity with a validation error message string to mark it as invalid.

We write

field.setCustomValidity("");

to call field.setCustomValidity with an empty string to mark it as valid.

field is an element like input or select

Conclusion

To mark a field invalid from JavaScript, we use the field element’s setCustomValidity method.

Categories
JavaScript Answers

How to select element that does not have specific class with JavaScript?

Sometimes, we want to select element that does not have specific class with JavaScript.

In this article, we’ll look at how to select element that does not have specific class with JavaScript.

How to select element that does not have specific class with JavaScript?

To select element that does not have specific class with JavaScript, we can use the document.querySelector method with the :not pseudoclass.

For instance, we write

const li = document.querySelector("li:not(.completed)");

to select the first li element that doesn’t have the completed class.

We can have multiple :not pseudoclasses in the same selector.

For instance, we write

const li = document.querySelector("li:not(.completed):not(.selected)");

to select the first li element that doesn’t have the completed or the selected class.

Conclusion

To select element that does not have specific class with JavaScript, we can use the document.querySelector method with the :not pseudoclass.