how to change font size on button click in javascript

Solutions on MaxInterview for how to change font size on button click in javascript by the best coders in the world

showing results for - "how to change font size on button click in javascript"
Joaquín
26 May 2019
1<script type="text/javascript">
2  var fontSizes = [
3    "xx-small",
4    "x-small",
5    "small",
6    "medium",
7    "large",
8    "x-large",
9    "xx-large"
10  ];
11  function getFontSize(id) {
12    return document.getElementById(id).style.fontSize || "medium";
13  }
14  function changeFontSize(id, size = "medium") {
15    document.getElementById(id).style.fontSize = size;
16  }
17  function cycleSize(id) {
18    let i = fontSizes.indexOf(getFontSize(id)) + 1;
19    changeFontSize(id, fontSizes[i >= fontSizes.length ? 0 : i]);
20  }
21</script>
22<p id="p">Click the button below to change my size!</p>
23<button onclick="cycleSize('p')">Change size</button>
similar questions
button size html