Categories
JavaScript Answers

How to remove attribute checked from checkbox with JavaScript?

Sometimes, we want to remove attribute checked from checkbox with JavaScript.

In this article, we’ll look at how to remove attribute checked from checkbox with JavaScript.

How to remove attribute checked from checkbox with JavaScript?

To remove attribute checked from checkbox with JavaScript, we use the removeAttribute method.

For instance, we write

const audioCheckbox = document.getElementById("audioCheckbox");

audioCheckbox.removeAttribute("checked");

to get the checkbox with getElementById.

Then we call removeAttribute on the audioCheckbox to remove the 'checked' attribute.

Conclusion

To remove attribute checked from checkbox with JavaScript, we use the removeAttribute method.

Categories
JavaScript Answers

How to convert Map to JSON object in JavaScript?

Sometimes, we want to convert Map to JSON object in JavaScript.

In this article, we’ll look at how to convert Map to JSON object in JavaScript.

How to convert Map to JSON object in JavaScript?

To convert Map to JSON object in JavaScript, we use the Object.fromEntries method.

For instance, we write

const map1 = new Map([
  ["foo", "qux"],
  ["baz", 100],
]);

const obj = Object.fromEntries(map1);

to create map1 with the Map constructor.

Then we call Object.fromEntries to return an array of key-value pair arrays from map1.

To convert the obj object back to a map, we write

const map2 = new Map(Object.entries(obj));
Categories
JavaScript Answers

How to get max and min value from array in JavaScript?

Sometimes, we want to get max and min value from array in JavaScript.

In this article, we’ll look at how to get max and min value from array in JavaScript.

How to get max and min value from array in JavaScript?

To get max and min value from array in JavaScript, we use some math methods.

For instance, we write

const array = [1, 3, 2];
const max = Math.max(...array);
const min = Math.min(...array);

to spread the entries of array as arguments of Math.max and Math.min.

max returns the max value in the list of arguments.

min returns the max value in the list of arguments.

Conclusion

To get max and min value from array in JavaScript, we use some math methods.

Categories
JavaScript Answers

How to set hours, minutes, seconds for Date which is in GMT with JavaScript?

Sometimes, we want to set hours, minutes, seconds for Date which is in GMT with JavaScript.

In this article, we’ll look at how to set hours, minutes, seconds for Date which is in GMT with JavaScript.

How to set hours, minutes, seconds for Date which is in GMT with JavaScript?

To set hours, minutes, seconds for Date which is in GMT with JavaScript, we use some date methods.

For instance, we write

const today = new Date();

const myToday = new Date(
  today.getFullYear(),
  today.getMonth(),
  today.getDate(),
  0,
  0,
  0
);

to create the today date object.

Then we create the myToday object that sets the hour, minute, and second to 0 and the rest of the values comes from today.

getFullYear returns the 4 digit year.

getMonth returns the month from 0 for January and 11 for December.

getDate returns the date of the month.

Conclusion

To set hours, minutes, seconds for Date which is in GMT with JavaScript, we use some date methods.

Categories
JavaScript Answers

How to hide a mobile browser’s address bar with JavaScript?

Sometimes, we want to hide a mobile browser’s address bar with JavaScript.

In this article, we’ll look at how to hide a mobile browser’s address bar with JavaScript.

How to hide a mobile browser’s address bar with JavaScript?

To hide a mobile browser’s address bar with JavaScript, we scroll down the page slightly.

For instance, we write

window.addEventListener("load", () => {
  setTimeout(() => {
    window.scrollTo(0, 1);
  }, 0);
});

to scroll down the page when it’s loaded.

We listen to the load event with addEventListener.

Then we call window.scrollTo to scroll down the page by 1 pixel in the setTimeout callback when the load event is triggered when the page is loaded to hide the address bar.

Conclusion

To hide a mobile browser’s address bar with JavaScript, we scroll down the page slightly.