Categories
JavaScript Answers

How to remove URL parameters with JavaScript?

Sometimes, we want to remove URL parameters with JavaScript.

In this article, we’ll look at how to remove URL parameters with JavaScript.

How to remove URL parameters with JavaScript?

To remove URL parameters with JavaScript, we use the string replace method.

For instance, we write

const onlyUrl = window.location.href.replace(window.location.search, "");

to call window.location.href.replace to replace the window.location.search query string with an empty string.

Conclusion

To remove URL parameters with JavaScript, we use the string replace method.

Categories
JavaScript Answers

How to style element create with createElement with JavaScript?

Sometimes, we want to style element create with createElement with JavaScript.

In this article, we’ll look at how to style element create with createElement with JavaScript.

How to style element create with createElement with JavaScript?

To style element create with createElement with JavaScript, we set the style.cssText property.

For instance, we write

const obj = document.createElement("div");
obj.id = "img";
obj.style.cssText = `
  position: absolute;
  top: 300px;
  left: 300px;
  width: 200px;
  height: 200px;
  -moz-border-radius: 100px;
  border: 1px solid #ddd;
  -moz-box-shadow: 0px 0px 8px #fff;
  display: none;
`;

to set the style.cssText property of the obj element to a string with the styles we want to apply.

Conclusion

To style element create with createElement with JavaScript, we set the style.cssText property.

Categories
JavaScript Answers

How to hide labels on y axis in Chart.js with JavaScript?

Sometimes, we want to hide labels on y axis in Chart.js with JavaScript.

In this article, we’ll look at how to hide labels on y axis in Chart.js with JavaScript.

How to hide labels on y axis in Chart.js with JavaScript?

To hide labels on y axis in Chart.js with JavaScript, we set the display property.

For instance, we write

const ctx = document.getElementById("log");
const chart = new Chart(ctx, {
  type: "line",
  options: {
    scales: {
      xAxes: [
        {
          display: false,
        },
      ],
      yAxes: [
        {
          display: false,
        },
      ],
    },
  },
  data: {
    labels: ["Item 1", "Item 2", "Item 3"],
    datasets: [
      {
        fill: false,
        borderWidth: 1,
        data: [10, 20, 30],
      },
    ],
  },
});

to create a chart with the Chart constructor by calling it with an object that sets the options.scale.xAxes to an array with an object that has display set to false to hide the x-axis labels.

Likewise, we do the same with yAxes to hide the y-axis labels.

Conclusion

To hide labels on y axis in Chart.js with JavaScript, we set the display property.

Categories
JavaScript Answers

How to find first index of a string after index with JavaScript?

Sometimes, we want to find first index of a string after index with JavaScript.

In this article, we’ll look at how to find first index of a string after index with JavaScript.

How to find first index of a string after index with JavaScript?

To find first index of a string after index with JavaScript, we call indexOf with the 2nd argument.

For instance, we write

const index = str.indexOf("s", startIndex);

to call indexOf to find the first 's' at startIndex or after.

Conclusion

To find first index of a string after index with JavaScript, we call indexOf with the 2nd argument.

Categories
JavaScript Answers

How to get the selected radio button value using JavaScript?

Sometimes, we want to get the selected radio button value using JavaScript.

In this article, we’ll look at how to get the selected radio button value using JavaScript.

How to get the selected radio button value using JavaScript?

To get the selected radio button value using JavaScript, we use the :checked pseudoselector.

For instance, we write

<p>Gender</p>
<input type="radio" id="gender0" name="gender" value="Male" />Male<br />
<input type="radio" id="gender1" name="gender" value="Female" />Female<br />

to add radio buttons.

Then we write

const gender = document.querySelector('input[name = "gender"]:checked').value;

to select the selected radio button with name attribute gender with querySelector.

And we get its value with value.

Conclusion

To get the selected radio button value using JavaScript, we use the :checked pseudoselector.