Categories
JavaScript Answers

How to check a radio button with JavaScript?

Sometimes, we want to check a radio button with JavaScript.

In this article, we’ll look at how to check a radio button with JavaScript.

How to check a radio button with JavaScript?

To check a radio button with JavaScript, we can set the checkbox’s checked property to true.

For instance, we write

<input type="radio" name="main-categories" id="_1234" value="1234" />
<input type="radio" name="main-categories" id="_2345" value="2345" />
<input type="radio" name="main-categories" id="_3456" value="3456" />
<input type="radio" name="main-categories" id="_4567" value="4567" />

to add 4 radio buttons.

Then we write

document.getElementById("_1234").checked = true;

to select the checkbox we want to check with getElementById.

And we set its checked property to true to check it.

Conclusion

To check a radio button with JavaScript, we can set the checkbox’s checked property to true.

Categories
JavaScript Answers

How to replace objects in array with JavaScript?

Sometimes, we want to replace objects in array with JavaScript.

In this article, we’ll look at how to replace objects in array with JavaScript.

How to replace objects in array with JavaScript?

To replace objects in array with JavaScript, we can use the array map method.

For instance, we write

const mapped = arr1.map((obj) => arr2.find((o) => o.id === obj.id) ?? obj);

to call arr1.map with a callback that maps the entry in arr1 to an object with the same id value in arr2 if it exists.

Otherwise, it returns the obj object in arr1.

Conclusion

To replace objects in array with JavaScript, we can use the array map method.

Categories
JavaScript Answers

How to restrict input to allow only numbers and decimal point in a textbox with JavaScript?

Sometimes, we want to restrict input to allow only numbers and decimal point in a textbox with JavaScript.

In this article, we’ll look at how to restrict input to allow only numbers and decimal point in a textbox with JavaScript.

How to restrict input to allow only numbers and decimal point in a textbox with JavaScript?

To restrict input to allow only numbers and decimal point in a textbox with JavaScript, we can call the string match method to validate the input value.

For instance, we write

form.onsubmit = () => {
  return textarea.value.match(/^\d+(\.\d+)?$/);
};

to set the onsubmit property of the form element object to a function that returns whether the textarea‘s input value is valid.

We call match with /^\d+(\.\d+)?$/ to check if the text area’s input value has digits and a decimal point.

Conclusion

To restrict input to allow only numbers and decimal point in a textbox with JavaScript, we can call the string match method to validate the input value.

Categories
JavaScript Answers

How to dynamically set property of nested object with JavaScript?

Sometimes, we want to dynamically set property of nested object with JavaScript.

In this article, we’ll look at how to dynamically set property of nested object with JavaScript.

How to dynamically set property of nested object with JavaScript?

To dynamically set property of nested object with JavaScript, we can use the Lodash set method.

For instance, we write

_.set(obj, "foo.bar", "baz");

to call set with obj, 'foo.bar' and 'baz' to set obj.foo.bar to 'baz'.

The property will be created if it doesn’t exist.

Conclusion

To dynamically set property of nested object with JavaScript, we can use the Lodash set method.

Categories
JavaScript Answers

How to convert uint8 array to base64 encoded string with JavaScript?

Sometimes, we want to convert uint8 array to base64 encoded string with JavaScript.

In this article, we’ll look at how to convert uint8 array to base64 encoded string with JavaScript.

How to convert uint8 array to base64 encoded string with JavaScript?

To convert uint8 array to base64 encoded string with JavaScript, we can use the FileReader constructor.

For instance, we write

const base64Arraybuffer = async (data) => {
  const base64url = await new Promise((r) => {
    const reader = new FileReader();
    reader.onload = () => r(reader.result);
    reader.readAsDataURL(new Blob([data]));
  });
  return base64url.split(",", 2)[1];
};

//...

await base64Arraybuffer(new Uint8Array([1, 2, 3, 100, 200]));

to define the base64Arraybuffer function that returns a promise with the blob that we convert from the data the Uint8Array into a base64 string.

We create the reader FileReader object and then call readAsDataURL with the blob value to read the blob into a base64 string.

Then we call r with reader.result to return the reader.result base64 string as the resolve value.

Then we call base64Arraybuffer in an async function with a Unit8Array as its argument to read it into a base64 string.

Conclusion

To convert uint8 array to base64 encoded string with JavaScript, we can use the FileReader constructor.