Categories
JavaScript Answers

How to remove button from Highcharts and JavaScript?

Sometimes, we want to remove button from Highcharts and JavaScript.

In this article, we’ll look at how to remove button from Highcharts and JavaScript.

How to remove button from Highcharts and JavaScript?

To remove button from Highcharts and JavaScript, we can add new buttons.

For instance, we write

const chart = new Highcharts.Chart({
  chart: {
    renderTo: "container",
  },
  credits: {
    enabled: false,
  },
  xAxis: {
    categories: [
      "Jan",
      "Feb",
      "Mar",
      "Apr",
      "May",
      "Jun",
      "Jul",
      "Aug",
      "Sep",
      "Oct",
      "Nov",
      "Dec",
    ],
  },
  series: [
    {
      data: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    },
  ],
  exporting: {
    buttons: [
      {
        symbol: "diamond",
        x: -62,
        symbolFill: "#B5C9DF",
        hoverSymbolFill: "#779ABF",
        _titleKey: "printButtonTitle",
        onclick: () => {
          console.log("clicked");
        },
      },
    ],
  },
});

to add the exporting.buttons property to override the default buttons with our own button.

We set the fill with symbolFill and the fill on hover with hoverSymbolFill.

onclick is called when we click on the button.

Conclusion

To remove button from Highcharts and JavaScript, we can add new buttons.

Categories
JavaScript Answers

How to return an array of objects with the JavaScript array map method?

Sometimes, we want to return an array of objects with the JavaScript array map method.

In this article, we’ll look at how to return an array of objects with the JavaScript array map method.

How to return an array of objects with the JavaScript array map method?

To return an array of objects with the JavaScript array map method, we can return an object in the map callback.

For instance, we write

const rockets = [
  { country: "US", launches: 23 },
  { country: "China", launches: 16 },
  { country: "Europe", launches: 7 },
  { country: "India", launches: 4 },
  { country: "Japan", launches: 3 },
];

const launchOptimistic = rockets.map((elem) => {
  const { country, launches } = elem;
  return {
    country,
    launches: launches + 10,
  };
});

console.log(launchOptimistic);

to call rockets.map with a callback that returns an object with the new launches value and the same country value.

Conclusion

To return an array of objects with the JavaScript array map method, we can return an object in the map callback.

Categories
JavaScript Answers

How to disable submit button once it has been clicked with JavaScript?

Sometimes, we want to disable submit button once it has been clicked with JavaScript.

In this article, we’ll look at how to disable submit button once it has been clicked with JavaScript.

How to disable submit button once it has been clicked with JavaScript?

To disable submit button once it has been clicked with JavaScript, we set its disabled property to true.

For instance, we write

<form action="/" method="POST">
  <input name="txt" type="text" required />
  <button id="btn" type="submit">Post</button>
</form>

to add a form.

Then we write

const form = document.querySelector("form");

form.onsubmit = () => {
  const btn = document.getElementById("btn");
  btn.disabled = true;
  btn.innerText = "Posting...";
};

to select the form with querySelector.

Then we set its onsubmit property to a function that disables the button when we click the Post button.

In it, we select the button with getElementById.

Then we set its disabled property to true to disable the button.

Conclusion

To disable submit button once it has been clicked with JavaScript, we set its disabled property to true.

Categories
JavaScript Answers

How to delete “px” from 245px with JavaScript?

Sometimes, we want to delete "px" from 245px with JavaScript.

In this article, we’ll look at how to delete "px" from 245px with JavaScript.

How to delete "px" from 245px with JavaScript?

To delete "px" from 245px with JavaScript, we can use the parseInt function.

For instance, we write

const size = parseInt("245px", 10);

to call parseInt with '245px' and 10 to parse the numerical part of "245px"` into a decimal integer.

10 is the radix to parse the number as.

The parsed number is returned.

Conclusion

To delete "px" from 245px with JavaScript, we can use the parseInt function.

Categories
JavaScript Answers

How to convert from Hex to ASCII in JavaScript?

Sometimes, we want to convert from Hex to ASCII in JavaScript.

In this article, we’ll look at how to convert from Hex to ASCII in JavaScript.

How to convert from Hex to ASCII in JavaScript?

To convert from Hex to ASCII in JavaScript, we can use some array and string methods.

For instance, we write

const asciiVal = "32343630"
  .match(/.{1,2}/g)
  .map((v) => {
    return String.fromCharCode(parseInt(v, 16));
  })
  .join("");

console.log(asciiVal);

to call the string match method to match groups of 2 digits and return an array with the matches.

Then we call map with a callback that gets the character code from the decimal number parsed from the hex number.

And then we call join to combine the array of characters into a string.

Conclusion

To convert from Hex to ASCII in JavaScript, we can use some array and string methods.