Categories
React Answers

How to use onClick with divs in React.js?

Sometimes, we want to use onClick with divs in React.js.

in this article, we’ll look at how to use onClick with divs in React.js.

How to use onClick with divs in React.js?

To use onClick with divs in React.js, we set the onClick prop to a function reference.

For instance, we write

<div onClick={doThis}>...</div>

or

<div onClick={() => doThis()}>...</div>

to set the onClick prop to a function.

We set it to the doThis function directly in the first example.

And we set it to a function that calls doThis in the 2nd example.

Conclusion

To use onClick with divs in React.js, we set the onClick prop to a function reference.

Categories
JavaScript Answers

How to define a JavaScript long integer?

Sometimes, we want to define a JavaScript long integer.

In this article, we’ll look at how to define a JavaScript long integer.

How to define a JavaScript long integer?

To define a JavaScript long integer, we can define a bigint.

For instance, we write

const rem = bigInt("1738141852226360940").mod("32").valueOf();
console.log(rem);

to call the bigInt function with a string with the integer we want to define.

Then we call mod to return the modulo of the bigint when divided by 32.

And then we call valueOf to convert the returned value to an integer.

Conclusion

To define a JavaScript long integer, we can define a bigint.

Categories
JavaScript Answers

How to encode a query string so that it is the value of another query string in JavaScript?

Sometimes, we want to encode a query string so that it is the value of another query string in JavaScript.

In this article, we’ll look at how to encode a query string so that it is the value of another query string in JavaScript.

How to encode a query string so that it is the value of another query string in JavaScript?

To encode a query string so that it is the value of another query string in JavaScript, we use the encodeURIComponent function.

For instance, we write

const c = "d e";
const query = "?a=b&c=" + encodeURIComponent(c);
const uri = "http://www.example.com/script?query=" + encodeURIComponent(query);

to call encodeURIComponent with c to return a URL encoded version of string c.

Then we append that to the "http://www.example.com/script?query=".

Conclusion

To encode a query string so that it is the value of another query string in JavaScript, we use the encodeURIComponent function.

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.