user1833620
user1833620

Reputation: 781

toggle 2 buttons that change font size

I have 2 buttons, and each button can change the body font size. I want to make the buttons that toggle. This is what I tried so far here [ https://jsfiddle.net/a6oscu8n/ ]

<style>
html { font-size:13px; }
html.big { font-size:18px; }
html.default { font-size:13px; }
</style>

<a href="#" data-size="big" class="font_resize">bigger</a><br> 
<a href="#" data-size="small" class="font_resize">default</a><br> 


texttexttexttext<br>  

$(function() { 
  $('.font_resize').on('click', function() {
    $('html')
      .attr('class', '')
      .addClass($(this).data('size'));
  }); 
});

Upvotes: 4

Views: 1377

Answers (2)

4b0
4b0

Reputation: 22323

You can toggle button using text. You also toggle between two class like below.

$(function() {
  $('.font_resize').on('click', function() {
    $(this).text(function(i, text) {
      return text === "bigger" ? "default" : "bigger";
    })
    $('html').toggleClass("small big");
  });
});
html {
  font-size: 13px;
}

html.big {
  font-size: 18px;
}

html.default {
  font-size: 13px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" data-size="big" class="font_resize">bigger</a><br>
<br> texttexttexttext

Upvotes: 3

Poul Bak
Poul Bak

Reputation: 10930

if you only have one button, then you can use Jquery toggleClass to do the trick, like this:

$(function() { 
  $('.font_resize').on('click', function() {
    $('html')
      .attr('class', '')
      .toggleClass('big', !$('html').has('big'));
  }); 
});

This will toggle the class between 'big' and 'nothing' (which will be default).

Upvotes: 2

Related Questions