Categories
JavaScript Answers

How to add a button click counter with JavaScript?

Sometimes, we want to add a button click counter with JavaScript.

In this article, we’ll look at how to add a button click counter with JavaScript.

How to add a button click counter with JavaScript?

To add a button click counter with JavaScript, we can add a click handler to a button.

For instance, we write:

<button>Click me</button>
<p>Clicks: <span id="clicks">0</span></p>

to add a button and a clicks display element.

Then we write:

const button = document.querySelector('button')
const span = document.querySelector('span')

let clicks = 0;

button.onclick = () => {
  clicks += 1;
  span.innerHTML = clicks;
};

to select the button and the span with querySelector.

Next, we set button.onclick to a click event handler function.

In it, we increment clicks by 1.

And we set span.innerHTML to clicks.

As a result, when we click the button, we show the number of clicks.

Conclusion

To add a button click counter with JavaScript, we can add a click handler to a button.

Categories
JavaScript Answers

How to sort an array of arrays in JavaScript?

Sometimes, we want to sort an array of arrays in JavaScript.

In this article, we’ll look at how to sort an array of arrays in JavaScript.

How to sort an array of arrays in JavaScript?

To sort an array of arrays in JavaScript, we can use the array sort method.

For instance, we write:

const array = [
  [123, 3],
  [745, 4],
  [643, 5],
  [643, 2]
];

const sortedArray = array.sort(([a], [b]) => {
  return b - a;
});

console.log(sortedArray)

We call array.sort with a callback that destructures the first entry from each nested array.

Then we return the value to determine how it’s sorted.

We rerturned b - a, so the returned sorted array will have the first entry sorted in descending order.

As a result, sortedArray is [[745, 4], [643, 5], [643, 2], [123, 3]] .

Conclusion

To sort an array of arrays in JavaScript, we can use the array sort method.

Categories
JavaScript Answers

How to remove all session storage items with keys that match a certain pattern with JavaScript?

Sometimes, we want to remove all session storage items with keys that match a certain pattern with JavaScript.

In this article, we’ll look at how to remove all session storage items with keys that match a certain pattern with JavaScript.

How to remove all session storage items with keys that match a certain pattern with JavaScript?

To remove all session storage items with keys that match a certain pattern with JavaScript, we can loop through the keys of the items we want to remove and remove the entries with the keys.

For instance, we write:

const keys = Object.keys(sessionStorage)
  .filter((k) => {
    return /foo/.test(k);
  })

for (const k of keys) {
  sessionStorage.removeItem(k);
}

to get the keys of the items we want to remove with:

const keys = Object.keys(sessionStorage)
  .filter((k) => {
    return /foo/.test(k);
  })

Then we loop through the keys with a for-of loop.

In it, we call sessionStorage.removeItem with key k to remove the entry with k as the key.

Conclusion

To remove all session storage items with keys that match a certain pattern with JavaScript, we can loop through the keys of the items we want to remove and remove the entries with the keys.

Categories
JavaScript Answers

How to get the mouse click coordinates for image with JavaScript?

Sometimes, we want to get the mouse click coordinates for image with JavaScript.

In this article, we’ll look at how to get the mouse click coordinates for image with JavaScript.

How to get the mouse click coordinates for image with JavaScript?

To get the mouse click coordinates for image with JavaScript, we can add a click event listener to the img element.

For instance, we write:

<img src="https://picsum.photos/200/300">

to add an img element.

Then we write:

const img = document.querySelector("img")
img.onclick = (e) => {
  const x = e.pageX - e.target.offsetLeft;
  const y = e.pageY - e.target.offsetTop;
  console.log(x, y);
}

to select the img element with querySelector.

Then we set img.onclick to a function that gets the x and y coordinates of the mouse click in the page with e.pageX - e.target.offsetLeft and e.pageY - e.target.offsetTop.

Conclusion

To get the mouse click coordinates for image with JavaScript, we can add a click event listener to the img element.

Categories
JavaScript Answers

How to change textbox border color using JavaScript?

Sometimes, we want to change textbox border color using JavaScript.

In this article, we’ll look at how to change textbox border color using JavaScript.

How to change textbox border color using JavaScript?

To change textbox border color using JavaScript, we can set the style.border property of the input to red.

For instance, we write:

<input type="text" />

to add an input.

Then we write:

document.querySelector("input").style.border = "1px solid red";

to select the input with querySelector.

And we set the style.border property of it to '1px solider red' to add a red border to it.

Conclusion

To change textbox border color using JavaScript, we can set the style.border property of the input to red.