Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A jquery function to decrease font size each time a button is pressed

Tags:

html

jquery

css

Please help.

function fontDown() {
  var curSize = parseInt($('.box').css('font-size'));
  curSize--;
  $('.box').css('font-size', curSize + "pt").css('line-height', curSize + "pt");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button onclick="fontDown()">button</button>
    
<div class="box" style="font-size: 14pt; line-height: 14pt;">sometext</div>

I expect the font-size to decrease each time I press the button. But, it only increases. Not only that but the font size increases by about 5 or 7 points each time the button is pressed.

like image 381
Joshua Robison Avatar asked Apr 18 '26 14:04

Joshua Robison


1 Answers

use this: it easy

operators +=, *=, -=, /= should be used in jquery

function fontDown() {
  $('.box')
    .css('font-size', '-=1pt')
    .css('line-height', '-=1pt');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button onclick="fontDown()">button</button>
    
<div class="box" style="font-size: 14pt; line-height: 14pt;">sometext</div>
like image 190
Tachibana Shin Avatar answered Apr 21 '26 04:04

Tachibana Shin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!