Categories
JavaScript Answers

How to get HTML “data-” attribute value with JavaScript?

Sometimes, we want to get HTML "data-" attribute value with JavaScript.

In this article, we’ll look at how to get HTML "data-" attribute value with JavaScript.

How to get HTML "data-" attribute value with JavaScript?

To get HTML "data-" attribute value with JavaScript, we use the getAttribute method.

For instance, we write

const one = obj.getAttribute("data-uid");

to call getAttribute on element obj to get the value of the data-uid attribute.

Conclusion

To get HTML "data-" attribute value with JavaScript, we use the getAttribute method.

Categories
JavaScript Answers

How to get value of input type file with JavaScript?

Sometimes, we want to get value of input type file with JavaScript.

In this article, we’ll look at how to get value of input type file with JavaScript.

How to get value of input type file with JavaScript?

To get value of input type file with JavaScript, we use the files property.

For instance, we write

const file = document.forms["formName"]["inputName"].files[0];

to get the file input with name attribute inputName in the form with name attribute formName with document.forms["formName"]["inputName"].

Then we get the selected files from the files property.

And we get the first file selected with index 0.

Conclusion

To get value of input type file with JavaScript, we use the files property.

Categories
JavaScript Answers

How to change or get check state of checkbox with JavaScript?

Sometimes, we want to change or get check state of checkbox with JavaScript.

In this article, we’ll look at how to change or get check state of checkbox with JavaScript.

How to change or get check state of checkbox with JavaScript?

To change or get check state of checkbox with JavaScript, we can use the checked property.

For instance, we write

<input type="checkbox" id="cbxTodos" name="cbxTodos" />

to add a checkbox.

Then we write

const elCheckBox = document.getElementById("cbxTodos");

elCheckBox.onchange = () => {
  console.log(elCheckBox.checked);
};

to select the checkbox with getElementById.

Then we set its onchange property to a function that’s called when the checkbox changes value.

In it, we get the checked value with the checked property.

checked can also be set to change the checked state.

Conclusion

To change or get check state of checkbox with JavaScript, we can use the checked property.

Categories
JavaScript Answers

How to read GET data from a URL using JavaScript?

Sometimes, we want to read GET data from a URL using JavaScript.

In this article, we’ll look at how to read GET data from a URL using JavaScript.

How to read GET data from a URL using JavaScript?

To read GET data from a URL using JavaScript, we use the URLSearchParams constructor.

For instance, we write

const params = new URLSearchParams(location.search);
const name = params.get("name");
const names = params.getAll("name");

to create a URLSearchParams object with the query string part of current URL.

Then we call get with the key name to return the first value with the key.

And we call getAll with the key name to return the all values with the key.

Conclusion

To read GET data from a URL using JavaScript, we use the URLSearchParams constructor.

Categories
JavaScript Answers

How to use a regular expression for finding decimal or float numbers with JavaScript?

Sometimes, we want to use a regular expression for finding decimal or float numbers with JavaScript.

In this article, we’ll look at how to use a regular expression for finding decimal or float numbers with JavaScript.

How to use a regular expression for finding decimal or float numbers with JavaScript?

To use a regular expression for finding decimal or float numbers with JavaScript, we use a regex that matches digits, signs and decimal points.

For instance, we write

/^[+-]?\d+(\.\d+)?$

to write a regex that matches the plus or minus sign at the beginning of the string with [+-].

We match digits that comes after it with \d+.

And we match the fractional part with (\.\d+)?.

Conclusion

To use a regular expression for finding decimal or float numbers with JavaScript, we use a regex that matches digits, signs and decimal points.