Categories
JavaScript Answers

How to detect if a page has a vertical scrollbar with JavaScript?

Sometimes, we want to detect if a page has a vertical scrollbar with JavaScript.

In this article, we’ll look at how to detect if a page has a vertical scrollbar with JavaScript.

How to detect if a page has a vertical scrollbar with JavaScript?

To detect if a page has a vertical scrollbar with JavaScript, we check if the scrollHeight of the element is bigger than its clientHeight.

For instance, we write

const hasVScroll = document.body.scrollHeight > document.body.clientHeight;

to check if the body element’s scrollHeight is bigger than its clientHeight to check if the body element has a vertical scrollbar.

Conclusion

To detect if a page has a vertical scrollbar with JavaScript, we check if the scrollHeight of the element is bigger than its clientHeight.

Categories
JavaScript Answers

How to set custom attribute using JavaScript?

Sometimes, we want to set custom attribute using JavaScript.

In this article, we’ll look at how to set custom attribute using JavaScript.

How to set custom attribute using JavaScript?

To set custom attribute using JavaScript, we can use the setAttribute method.

For instance, we write

document
  .getElementById("item1")
  .setAttribute(
    "data",
    "icon: 'base2.gif', url: 'output.htm', target: 'AccessPage', output: '1'"
  );

to select the element with ID item1 with getElementById.

Then we call setAttribute with the attribute name and value to set the attribute.

Conclusion

To set custom attribute using JavaScript, we can use the setAttribute method.

Categories
JavaScript Answers

How to clear a chart from a canvas so that hover events cannot be triggered with Chart.js and JavaScript?

Sometimes, we want to clear a chart from a canvas so that hover events cannot be triggered with Chart.js and JavaScript.

In this article, we’ll look at how to clear a chart from a canvas so that hover events cannot be triggered with Chart.js and JavaScript.

How to clear a chart from a canvas so that hover events cannot be triggered with Chart.js and JavaScript?

To clear a chart from a canvas so that hover events cannot be triggered with Chart.js and JavaScript, we call destroy to destroy the chart.

For instance, we write

let myPieChart = null;

//...

const drawChart = (objChart, data) => {
  if (myPieChart !== null) {
    myPieChart.destroy();
  }
  const ctx = objChart.getContext("2d");
  myPieChart = new Chart(ctx).Pie(data, { animateScale: true });
};

to create the drawChart function that calls myPieChart.destroy if myPieChart isn’t null to clear the chart.

Then we get the objChart‘s canvas context with getContext.

And we set myPieChart to a new chart created within the ctx context.

Conclusion

To clear a chart from a canvas so that hover events cannot be triggered with Chart.js and JavaScript, we call destroy to destroy the chart.

Categories
JavaScript Answers

How to write nested functions in JavaScript?

Sometimes, we want to write nested functions in JavaScript.

In this article, we’ll look at how to write nested functions in JavaScript.

How to write nested functions in JavaScript?

To write nested functions in JavaScript, we can define functions inside functions.

For instance, we write

const a = (x) => {
  const b = (y) => {
    return x + y;
  };
  return b;
};
console.log(a(3)(4));

to define function b in function a.

We return function b in the last line of a.

And then we call a with 3 to return b.

Then we call b with 4 to get 7.

Conclusion

To write nested functions in JavaScript, we can define functions inside functions.

Categories
JavaScript Answers

How to get the nth occurrence in a string with JavaScript?

Sometimes, we want to get the nth occurrence in a string with JavaScript.

In this article, we’ll look at how to get the nth occurrence in a string with JavaScript.

How to get the nth occurrence in a string with JavaScript?

To get the nth occurrence in a string with JavaScript, we can use the split and join methods.

For instance, we write

const string = "XYZ 123 ABC 456 ABC 789 ABC";

const getPosition = (string, subString, index) => {
  return string.split(subString, index).join(subString).length;
};

console.log(
  getPosition(string, "ABC", 2)
);

to define the getPosition function.

In it, we call string.split with subString and index to split the string starting at index with subString as the separator.

Then we call join with subString to join the returned string array back into a string and get its length.

Then we get the start index of the 2nd 'ABC' with getPosition(string, "ABC", 2).

Conclusion

To get the nth occurrence in a string with JavaScript, we can use the split and join methods.