Categories
JavaScript Answers

How to loop through a set of array elements in JavaScript?

Sometimes, we want to loop through a set of array elements in JavaScript.

In this article, we’ll look at how to loop through a set of array elements in JavaScript.

How to loop through a set of array elements in JavaScript?

To loop through a set of array elements in JavaScript, we use a for-of loop.

For instance, we write

const list = [
  { a: 1, b: 2 },
  { a: 3, b: 5 },
  { a: 8, b: 2 },
  { a: 4, b: 1 },
  { a: 0, b: 8 },
];

for (const item of list) {
  console.log(item);
}

to loop through the items in list with a for-of loop.

We get the item being looped through from item.

Conclusion

To loop through a set of array elements in JavaScript, we use a for-of loop.

Categories
JavaScript Answers

How to get one item from an array with the name name and value with JavaScript?

Sometimes, we want to get one item from an array with the name name and value with JavaScript.

In this article, we’ll look at how to get one item from an array with the name name and value with JavaScript.

How to get one item from an array with the name name and value with JavaScript?

To get one item from an array with the name name and value with JavaScript, we use the array find method.

For instance, we write

const arr = [];
arr.push({ name: "k1", value: "abc" });
arr.push({ name: "k2", value: "hi" });
arr.push({ name: "k3", value: "oa" });

const found = arr.find((item) => {
  return item.name === "k1";
});

console.log("found", found);

to call arr.find with a callback that checks for the first object in arr with name property 'k1'.

Then found has the item as its value.

Conclusion

To get one item from an array with the name name and value with JavaScript, we use the array find method.

Categories
JavaScript Answers

How to get caret position in HTML input with JavaScript?

Sometimes, we want to get caret position in HTML input with JavaScript

In this article, we’ll look at how to get caret position in HTML input with JavaScript.

How to get caret position in HTML input with JavaScript?

To get caret position in HTML input with JavaScript, we listen to the keydown event.

For instance, we write

<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />

    <script type="text/javascript">
      window.addEventListener("load", () => {
        const [input] = document.getElementsByTagName("input");

        input.addEventListener("keydown", (e) => {
          console.log("Caret position: ", e.target.selectionStart);
        });
      });
    </script>

    <title>Test</title>
  </head>

  <body>
    <input type="text" />
  </body>
</html>

to add an input in the body element.

Then we get the input in the load event listener with getElementsByTagName.

Then we call input.addEventListener with 'keydown' to listen to the keydown event which is emitted as we type.

And then we get the current caret position with e.target.selectionStart.

Conclusion

To get caret position in HTML input with JavaScript, we listen to the keydown event.

Categories
JavaScript Answers

How to fix string.replace doesn’t change the variable with JavaScript?

Sometimes, we want to fix string.replace doesn’t change the variable with JavaScript.

In this article, we’ll look at how to fix string.replace doesn’t change the variable with JavaScript.

How to fix string.replace doesn’t change the variable with JavaScript?

To fix string.replace doesn’t change the variable with JavaScript, we assign the returned string to a new variable.

For instance, we write

variableABC = variableABC.replace(/B/g, "D");

to call variableABC.replace with the regex and 'D' to replace all B’s with D’s and return the string with the replacements.

Then we assign the returned value back to variableABC to update it.

Conclusion

To fix string.replace doesn’t change the variable with JavaScript, we assign the returned string to a new variable.

Categories
JavaScript Answers

How to add elements to the DOM given plain text HTML using only pure JavaScript?

Sometimes, we want to add elements to the DOM given plain text HTML using only pure JavaScript.

In this article, we’ll look at how to add elements to the DOM given plain text HTML using only pure JavaScript.

How to add elements to the DOM given plain text HTML using only pure JavaScript?

To add elements to the DOM given plain text HTML using only pure JavaScript, we call the insertAdjancentHTML methods.

For instance, we write

document.body.insertAdjacentHTML("beforeend", theHTMLToInsert);

to call document.body.insertAdjacentHTML to insert the content of the theHTMLToInsert before the end of the body element.

Conclusion

To add elements to the DOM given plain text HTML using only pure JavaScript, we call the insertAdjancentHTML methods.