Categories
JavaScript Answers

How to add an item to dropdown list in HTML using JavaScript?

Sometimes, we want to add an item to dropdown list in HTML using JavaScript.

In this article, we’ll look at how to add an item to dropdown list in HTML using JavaScript.

How to add an item to dropdown list in HTML using JavaScript?

To add an item to dropdown list in HTML using JavaScript, we can use a for loop.

For instance, we write:

<select>

</select>

to add a select element.

Then we write:

const select = document.querySelector("select");
for (let i = 2022; i >= 1900; i--) {
  const option = document.createElement('option');
  option.text = option.value = i;
  select.add(option, 0);
}

to select the select element with document.querySelector.

And then we use a for loop to loop from 2022 to 1900.

In the loop, we create the option element with:

const option = document.createElement('option');

Then we set the text and value to set the text and the value attribute value respectively.

And then we call select.add to add the option into the select.

Conclusion

To add an item to dropdown list in HTML using JavaScript, we can use a for loop.

Categories
JavaScript Answers

How to manually show a HTML5 validation message from a JavaScript function?

Sometimes, we want to manually show a HTML5 validation message from a JavaScript function.

In this article, we’ll look at how to manually show a HTML5 validation message from a JavaScript function.

How to manually show a HTML5 validation message from a JavaScript function?

To manually show a HTML5 validation message from a JavaScript function, we can call the reportValidity method.

For instance, we write:

<form>
  <input type="email" id="email" placeholder="email" required>
  <button type="submit">Submit</button>
</form>

to add a form.

Then we write:

const form = document.querySelector("form");
if (form.checkValidity()) {
  form.submit();
} else {
  form.reportValidity();
}

We select the form with document.querySelector.

Then we call checkValidity to validate the form values.

Then we call reportValidity when there are invalid form values.

As a result, we should see form validation messages.

Conclusion

To manually show a HTML5 validation message from a JavaScript function, we can call the reportValidity method.

Categories
JavaScript Answers

How to round down number 2 decimal places with JavaScript?

Sometimes, we want to round down number 2 decimal places with JavaScript.

In this article, we’ll look at how to round down number 2 decimal places with JavaScript.

How to round down number 2 decimal places with JavaScript?

To round down number 2 decimal places with JavaScript, we can multiply the number we want to round by 100, take the floor of the product with Math.floor, and then divide the floor with 100.

For instance, we write:

const n = Math.floor(1.5675675 * 100) / 100
console.log(n)

We want to round 1.5675675 down to 2 decimal places.

To do this, we multiply 1.5675675 by 100.

Then we call Math.floor on the product to take the floor.

And then we divided that by 100.

Therefore, n is 1.56.

Conclusion

To round down number 2 decimal places with JavaScript, we can multiply the number we want to round by 100, take the floor of the product with Math.floor, and then divide the floor with 100.

Categories
JavaScript Answers

How to add members to an existing object with JavaScript?

Sometimes, we want to add members to an existing object with JavaScript.

In this article, we’ll look at how to add members to an existing object with JavaScript.

How to add members to an existing object with JavaScript?

To add members to an existing object with JavaScript, we can assign a value to a property in the object.

For instance, we write:

const obj = {
  fn1: () => {}
}
obj.fn2 = () => {};
console.log(obj)

We set obj.fn2 to a function to add fn2 as a property of obj.

Conclusion

To add members to an existing object with JavaScript, we can assign a value to a property in the object.

Categories
JavaScript Answers

How to convert integers to hex string in JavaScript?

Sometimes, we want to convert integers to hex string in JavaScript.

In this article, we’ll look at how to convert integers to hex string in JavaScript.

How to convert integers to hex string in JavaScript?

To convert integers to hex string in JavaScript, we can use the number’s toString method.

For instance, we write:

const g = 255;
const gPadded = Number(g).toString(16).padStart(2, "0");
console.log(gPadded)

to convert g to a number with Number.

Then we call toString with 16 to convert the number to a hex string.

Finally, we call padStart with length 2 and string '0' to pad the string to 2 characters with 0’s at the start of the string.

So gPadded is 'ff'.

Conclusion

To convert integers to hex string in JavaScript, we can use the number’s toString method.