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
HTML

How to impose maxlength on text area in HTML?

Sometimes, we want to impose maxlength on text area in HTML using JavaScript.

In this article, we’ll look at how to impose maxlength on text area in HTML using JavaScript.

How to impose maxlength on text area in HTML?

To impose maxlength on text area in HTML using JavaScript, we can set the maxlength attribute of the input.

For instance, we write

<!DOCTYPE html>
<html>
  <body>
    <form action="processForm.php" action="post">
      <label for="story">Tell me your story:</label><br />
      <textarea id="story" maxlength="100"></textarea>
      <input type="submit" value="Submit" />
    </form>
  </body>
</html>

to set the maxlength attribute of the textarea element to 100 characters.

Conclusion

To impose maxlength on text area in HTML using JavaScript, we can set the maxlength attribute of the input.

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.