Answers for "how to change font size on button click in javascript"

0

how to change font size on button click in javascript

<script type="text/javascript">
  var fontSizes = [
    "xx-small",
    "x-small",
    "small",
    "medium",
    "large",
    "x-large",
    "xx-large"
  ];
  function getFontSize(id) {
    return document.getElementById(id).style.fontSize || "medium";
  }
  function changeFontSize(id, size = "medium") {
    document.getElementById(id).style.fontSize = size;
  }
  function cycleSize(id) {
    let i = fontSizes.indexOf(getFontSize(id)) + 1;
    changeFontSize(id, fontSizes[i >= fontSizes.length ? 0 : i]);
  }
</script>
<p id="p">Click the button below to change my size!</p>
<button onclick="cycleSize('p')">Change size</button>
Posted by: Guest on July-16-2021

Code answers related to "how to change font size on button click in javascript"

Browse Popular Code Answers by Language