I'm having trouble adjusting the font size of the bodytext
div. When using the code snippet below on a page with various formatting styles, and nested inside a <div id="bodytext">
, it doesn't seem to work as intended. It ends up altering line spacing instead of the font size itself.
Functioning Code:
var $affectedElements = $("#bodytext");
$affectedElements.children().each( function(){
var $this = $(this);
$this.data("orig-size", $this.css("font-size") );
});
$("#btn-increase").click(function(){
changeFontSize(1);
})
$("#btn-decrease").click(function(){
changeFontSize(-1);
})
$("#btn-orig").click(function(){
$affectedElements.children().each( function(){
var $this = $(this);
$this.css( "font-size" , $this.data("orig-size") );
});
})
function changeFontSize(direction){
$affectedElements.children().each( function(){
var $this = $(this);
$this.css( "font-size" , parseInt($this.css("font-size"))+direction );
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<button id="btn-decrease">A-</button>
<button id="btn-orig">A</button>
<button id="btn-increase">A+</button>
<div id="bodytext">
<p style="font-size : 30px">This text is initially 30px</p>
<p style="font-size : 20px">This text is initially 20px</p>
<p style="font-size : 10px">This text is initially 10px</p>
</div>
</div>
Issue (that requires correction):
var $affectedElements = $("#bodytext");
$affectedElements.children().each( function(){
var $this = $(this);
$this.data("orig-size", $this.css("font-size") );
});
$("#btn-increase").click(function(){
changeFontSize(1);
})
$("#btn-decrease").click(function(){
changeFontSize(-1);
})
$("#btn-orig").click(function(){
$affectedElements.children().each( function(){
var $this = $(this);
$this.css( "font-size" , $this.data("orig-size") );
});
})
function changeFontSize(direction){
$affectedElements.children().each( function(){
var $this = $(this);
$this.css( "font-size" , parseInt($this.css("font-size"))+direction );
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<button id="btn-decrease">A-</button>
<button id="btn-orig">A</button>
<button id="btn-increase">A+</button>
<div id="bodytext">
Suitable alongside several formatting styles that require modifications for consistency.
Fetching the original data tied to sizing elements allows for easy reset options.
Offer valid clues or insights rather than encrypted guesswork potential.
Encourage team playing and positive reinforcements when formerly faced with distress.