Categories
JavaScript Answers

How to change lowercase characters to uppercase using the keyup event with JavaScript?

Sometimes, we want to change lowercase characters to uppercase using the keyup event with JavaScript.

In this article, we’ll look at how to change lowercase characters to uppercase using the keyup event with JavaScript.

How to change lowercase characters to uppercase using the keyup event with JavaScript?

To change lowercase characters to uppercase using the keyup event with JavaScript, we set the value to upper case with toUpperCase.

For instance, we write

const input = document.getElementById("inputID");

input.onkeyup = (e) => {
  e.target.value = e.target.value.toUpperCase();
};

to get the input with getElementById.

Then we set its onkeyup property to convert the input value to upper case with toUpperCase.

We then assign the returned string back to e.target.value to update the string.

Conclusion

To change lowercase characters to uppercase using the keyup event with JavaScript, we set the value to upper case with toUpperCase.

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.