Categories
JavaScript Answers

How to create a ul and fill it based on a passed array with JavaScript?

Sometimes, we want to create a ul and fill it based on a passed array with JavaScript.

In this article, we’ll look at how to create a ul and fill it based on a passed array with JavaScript.

How to create a ul and fill it based on a passed array with JavaScript?

To create a ul and fill it based on a passed array with JavaScript, we can loop through the array and then add the li elements with the array entry content into the ul element.

For instance, we write

const options = {
  set0: ["Option 1", "Option 2"],
  set1: ["First Option", "Second Option", "Third Option"],
};

const list = "<li>" + options.set0.join("</li><li>") + "</li>";
document.getElementById("list").innerHTML = list;

to get the options.set0 array and then join the entries by calling join with "</li><li>".

And then we wrap the returned string with opening and closing li tags.

Next, we select the ul with getElementById.

And then we set its innerHTML property to list to render the li’s in the ul.

Conclusion

To create a ul and fill it based on a passed array with JavaScript, we can loop through the array and then add the li elements with the array entry content into the ul element.

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.