0

I have a button and it should increase the font size when clicked and if it's clicked again it should decrease the font. Also the value of the button should change to Increase Font / Decrease Font. So, basically I want to make the button toggle from increasing the font to 16px then decreasing to 14px if clicked again.

EDIT: I made it work but it doesn't keep repeating. Only works twice and that's it

HTML

<p id="increase">Lorem Ipsum.</p>

<input onclick="font()" style="background-color:#72cf26"  type="submit" value="Increase Font" id = "fontbutton"/>

JS

function font(){
  var fontsize = document.getElementById('increase');
  var fontbutton = document.getElementById('fontbutton');

  if (fontbutton.value == "Increase Font"){
    fontsize.classList.add("font16");
    document.getElementById('fontbutton').value = "Decrease Font";
  }else if (fontbutton.value == "Decrease Font"){
    fontsize.classList.add("font14");
    document.getElementById('fontbutton').value = "Increase Font";
    }
 }

CS

.font16{
   font-size:16px;
}
.font14{
   font-size: 14px;
}
8
  • fontbutton.getElementById.value doesn't make sense. getElementById is a method, which you seem to understand as you use it correctly before this line. Commented May 15, 2020 at 18:18
  • How would I check the value of the button to make it work then in an if statement then? Commented May 15, 2020 at 18:22
  • are you ok to use jQuery? Commented May 15, 2020 at 18:24
  • What is default font size? Commented May 15, 2020 at 18:25
  • Remove the .getElementById from that line. You don't need that. You already have the button in the fontbutton variable. Just call .value on it. Commented May 15, 2020 at 18:26

4 Answers 4

3

You should use fontbutton.value instead of fontbutton.getElementById.value and remove the previous class using fontsize.classList.remove to add the new one using fontsize.classList.add:

function font(){
     var fontsize = document.getElementById('increase');
     var fontbutton = document.getElementById('fontbutton');
     if (fontbutton.value == "Increase Font"){
          fontsize.classList.remove("font14");
          fontsize.classList.add("font16");
          fontbutton.value = "Decrease Font";
     }else if (fontbutton.value == "Decrease Font"){
          fontsize.classList.remove("font16");
          fontsize.classList.add("font14");
          fontbutton.value = "Increase Font";
     }
}
.font16{
     font-size:16px;
}
.font14{
     font-size: 14px;
}
<p id="increase" class="font14">Lorem Ipsum.</p>
<input onclick="font()" style="background-color:#72cf26"  type="submit" value="Increase Font" id = "fontbutton"/>
          

Sign up to request clarification or add additional context in comments.

Comments

0

The above requirement can be implemented using

element.classlist.add and element.classlist.remove functions.

Here is a copy of the working code:

<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <style>
            .font16{
                font-size:16px;
                background-color:#72cf26;
            }
            .font14{
                font-size: 14px;
                background-color:#72cf26;
            }
        </style>
    </head>
    <body>
        <p id="increase">Lorem Ipsum.</p>
        <button id="fontbutton" class="font14" onclick="toggleFont()">
            Increase Font
        </button>
        <script>
            function toggleFont() {
                var element = document.getElementById("fontbutton")
                var buttonText = element.innerHTML
                element.classList.remove("font14")
                element.classList.remove("font16")
                if(buttonText.indexOf("Increase") >= 0) {
                    element.classList.add("font16")
                    element.innerHTML = "Decrease font"
                } else {
                    element.classList.add("font14")
                    element.innerHTML = "Increase font"
                }
            }
        </script>
    </body>
</html>

Output:

enter image description here

More information:

https://www.w3schools.com/howto/howto_js_toggle_class.asp

Comments

0

$(document).ready(function(){
		var flag = true;
		$('.btn').click(function(){
    		$(this).find('span').toggleClass('hidden');
    		if(flag) {
    			$('#increase').addClass('font16');
    			$('#increase').removeClass('font14');
    			flag = !flag;
    		}else {
    			$('#increase').removeClass('font16');
    			$('#increase').addClass('font14');
    			flag = !flag;
    		}
    	});
	});
.font16{
   font-size:16px;
}
.font14{
   font-size: 14px;
}
.hidden {
    		display: none;
    	}

p{ font-size: 10px;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p id="increase">Lorem Ipsum.</p>
    <button class="btn">
    	<span>Increse Font</span>
    	<span class="hidden">Decrese Font</span>
    </button>

Comments

0

Your approach is complicated, JS directly offers a toggle method to add / delete a class and which returns a boolean value.
The only difficulty is that you should not forget to add !important to upgrade the size.

const paragraphInc = document.getElementById('increase')
  ,   buttonSize   = document.getElementById('fontbutton')
  ;
buttonSize.onclick = () =>
  {
  buttonSize.textContent = (paragraphInc.classList.toggle('Size16'))
                          ? 'Decrease Font'
                          : 'Increase Font'
  }
#fontbutton {
  background-color:#72cf26;
  padding: 7px 12px;
  }
#increase {
  font-size: 14px;
  }
.Size16 {
  font-size: 16px !important;
  }
<p id="increase">Lorem Ipsum.</p>

<button id="fontbutton">Increase Font</button>

1 Comment

To be pedantic for a moment — it is the DOM Element.classList object that offers the toggle method, that is not a feature of Javascript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.