Categories
CSS

How to style an HTML radio button to look like a checkbox with CSS?

Sometimes, we want to style an HTML radio button to look like a checkbox with CSS.

In this article, we’ll look at how to style an HTML radio button to look like a checkbox with CSS.

How to style an HTML radio button to look like a checkbox with CSS?

To style an HTML radio button to look like a checkbox with CSS, we use the appearance property.

For instance, we write

<label><input type="radio" name="radio" /> Checkbox 1</label>
<label><input type="radio" name="radio" /> Checkbox 2</label>

to add 2 checkboxes.

Then we write

input[type="radio"] {
  -webkit-appearance: checkbox; 
  -moz-appearance: checkbox; 
  -ms-appearance: checkbox; 
}

to add make radio buttons look like checkboxes by setting the appearance style property.

Conclusion

To style an HTML radio button to look like a checkbox with CSS, we use the appearance property.

Categories
JavaScript Answers

How to enumerate dates between two dates in Moment and JavaScript?

Sometimes, we want to enumerate dates between two dates in Moment and JavaScript.

In this article, we’ll look at how to enumerate dates between two dates in Moment and JavaScript.

How to enumerate dates between two dates in Moment and JavaScript?

To enumerate dates between two dates in Moment and JavaScript, we use a while loop.

For instance, we write

const enumerateDaysBetweenDates = (startDate, endDate) => {
  const now = startDate.clone();
  const dates = [];

  while (now.isSameOrBefore(endDate)) {
    dates.push(now.format("M/D/YYYY"));
    now.add(1, "days");
  }
  return dates;
};

to define the enumerateDaysBetweenDates function.

In it, we clone startDate with clone.

And then we call now.isSameOrBefore with endDate to check if now is before or same as endDate.

If it is, the while loop runs.

In it, we call dates.push to push the now date formatted into a string into dates.

Then we call now.add to add day tonow`.

Finally, we return the dates array.

Conclusion

To enumerate dates between two dates in Moment and JavaScript, we use a while loop.

Categories
JavaScript Answers

How to convert JavaScript array to CSV?

Sometimes, we want to convert JavaScript array to CSV.

In this article, we’ll look at how to convert JavaScript array to CSV.

How to convert JavaScript array to CSV?

To convert JavaScript array to CSV, we use array and object methods.

For instance, we write

const data = [
  { title: "Book title 1", author: "Name1 Surname1" },
  { title: "Book title 2", author: "Name2 Surname2" },
  { title: "Book title 3", author: "Name3 Surname3" },
  { title: "Book title 4", author: "Name4 Surname4" },
];

const csv = data
  .map((d) => {
    return Object.values(d).join(",");
  })
  .join("\n")

to call data.map with a callback that gets the values from each object in data with Object.values.

Then we call join to join the values into a string with each entry separated by a comma.

Then we call join again to combine the strings into a CSV with each line separated by a new line character.

Conclusion

To convert JavaScript array to CSV, we use array and object methods.

Categories
JavaScript Answers

How to convert 12 hour (AM/PM) string to 24 hour string using moment.js and JavaScript?

Sometimes, we want to convert 12 hour (AM/PM) string to 24 hour string using moment.js and JavaScript.

In this article, we’ll look at how to convert 12 hour (AM/PM) string to 24 hour string using moment.js and JavaScript.

How to convert 12 hour (AM/PM) string to 24 hour string using moment.js and JavaScript?

To convert 12 hour (AM/PM) string to 24 hour string using moment.js and JavaScript, we use the format method.

For instance, we write

const dt = moment("12:15 AM", ["h:mm A"]).format("HH:mm");

to create a moment object from the time string with moment.

Then we format it into a 24 hour time string by calling format with 'HH:mm'.

Conclusion

To convert 12 hour (AM/PM) string to 24 hour string using moment.js and JavaScript, we use the format method.

Categories
Angular Answers

How to set response type as text while making HTTP call with Angular?

Sometimes, we want to set response type as text while making HTTP call with Angular.

In this article, we’ll look at how to set response type as text while making HTTP call with Angular.

How to set response type as text while making HTTP call with Angular?

To set response type as text while making HTTP call with Angular, we set the responseType property.

For instance, we write

this.http
  .post(
    "http://localhost:8080/order/addtocart",
    { dealerId: 13, createdBy: "-1", productId, quantity },
    { headers, responseType: "text" }
  )
  .pipe(catchError(this.errorHandlerService.handleError));

to call http.post to make a post request.

We set responseType to 'text' so we get a string as the response body value.

Conclusion

To set response type as text while making HTTP call with Angular, we set the responseType property.