Reputation: 25
I would like to increase the font size of the paragraph as well as the font size of the number in the button.
I copied and pasted my sizer function from StackOverflow (a few alterations) and thought it would work and still can't get it to work. Can someone help?
Since I've spent so much time on just the first part, as a beginner programmer, I'm wondering what I am missing. Does anyone have any ideas from my code or their experience as to what I might be missing?
Thanks as always.
<html>
<button onclick='incrementer(); sizer()' id='count' value=0 />0</button>
<p id='test'>a</p>
<script>
clicks = 0
incrementer = function () {
clicks += 1
click = document.querySelector("#count").textContent = clicks;
click.innerHTML = document.getElementById("count").value = document.getElementById('test');
}
sizer = function changeFontSize() {
div = document.getElementById("test");
currentFont = div.style.fontSize.replace("pt", "");
div.style.fontSize = parseInt(currentFont) + parseInt(clicks) + "pt";
}
</script>
</html>
Upvotes: 2
Views: 1696
Reputation: 42267
Here's a from-scratch version that does what you're asking for. I'll point out a few things that I did to help you out.
https://codepen.io/anon/pen/VBPpZL?editors=1010
<html>
<body>
<button id="count">0</button>
<p id="test">
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</p>
</body>
</html>
JS:
window.addEventListener('load', () => {
const button = document.querySelector('#count');
const paragraph = document.querySelector('#test');
const startingFontSize = window.getComputedStyle(document.body, null)
.getPropertyValue('font-size')
.slice(0, 2) * 1;
let clicks = 0;
button.addEventListener('click', () => {
clicks++;
// this is a template literal
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
const fontSize = `${startingFontSize + clicks}px`;
button.innerHTML = clicks;
button.style.fontSize = fontSize;
paragraph.style.fontSize = fontSize;
});
});
The code runs when the page is loaded, so we attach an event listener on the window
object listening for the load
event.
We then store references to the button and the paragraph elements. These are const
variables because their values won't change. This also limits their scope to the containing function.
We get the initial font size for the body element, because in this example we aren't explicitly setting a base font in css so we're just using the one for the document. getComputedStyle
is a somewhat expensive operation, and in this case we only need to get it in the beginning because it won't change, so we also store it as a const
. The value is returned as a string like "16px" but we need the number, hence the slice
and multiplying by one to cast the string into a number. parseInt
would also do the same thing.
Notice that clicks
is defined with let
. This means that the variable can be changed. var
still works of course, but in modern practices its best to use const
and let
when declaring variables. This is partly because it forces you to think about what kind of data you're working with.
We add an event listener to the button
element and listen for the click event. First, we increment the clicks
variable. Then we declare fontSize
using a template literal which adds our new clicks
count to the startingFontSize
and "px" to get a string.
Finally, the innerHTML value of the button
element is updated. Then we update the fontStyle property for both elements.
Upvotes: 1
Reputation: 2682
Some things here:
onclick
here. Just append one and call your second function from the first one that gets fired via onclick
. That looks a lot more tidyvar
before every variable, without it's not valid JavaScriptcurrentFont
variable, so I removed it. It's not necessary and causes the script to not working correctly<html>
<button onclick='incrementer()' id='count' value=0 />0</button>
<p id='test'>a</p>
<script>
var clicks = 0;
var incrementer = function() {
clicks += 1;
var click = document.querySelector("#count").textContent = clicks;
click.innerHTML = document.getElementById("count").value = document.getElementById('test');
sizer();
}
var sizer = function changeFontSize() {
var div = document.getElementById("test");
div.style.fontSize = parseInt(clicks) + "pt";
}
</script>
</html>
Upvotes: 1
Reputation: 1626
I don't understand the logic of this solution, but you can simplify it avoiding to use a lot of var (anyway always prefer let or const if you don't need to change), using a single function and writing less code.
function increment(e){
const ctrl = document.getElementById('test');
let current = parseInt(e.dataset.size);
current += 1;
e.innerHTML = current;
e.dataset.size = current;
ctrl.style.fontSize = current + 'pt';
}
<button onclick="increment(this);" data-size="20">20</button>
<p id='test' style="font-size:20pt;">A</p>
Upvotes: 0
Reputation: 770
You don't have an initial font-size
style on your <p>
tag, so it div.style.fontSize is always empty. Also, best practice is to always use var
when introducing new variables in javascript.
One good trick to help debugging things like these is to use console.log()
at various points, and see whats coming out in your browser console. I used console.log(div.style.fontSize)
and the answer became clear.
Working below after adding <p style='font-size:12px'>a</p>
:
<html>
<button style='font-size:12px;' onclick='incrementer(); sizer()' id='count' value=0 />0</button>
<p id='test' style='font-size:12px;'>a</p>
<script>
var clicks = 0
incrementer = function () {
clicks += 1
click = document.querySelector("#count").textContent = clicks;
click.innerHTML = document.getElementById("count").value = document.getElementById('test');
}
var sizer = function changeFontSize() {
var div = document.getElementById("test");
var btn = document.getElementById("count");
var newSize = parseInt(div.style.fontSize.replace("pt", "")) + parseInt(clicks);
div.style.fontSize = newSize + "pt";
btn.style.fontSize = newSize + "pt";
}
</script>
</html>
Upvotes: 0
Reputation: 651
The issue here is that there is no initial value for the fontSize of your <p>
tag so div.style.fontSize returns an empty string.
You can use window.getComputedStyle instead of div.style.fontSize and you will get the current fontSize.
There is already a post explaining this method
https://stackoverflow.com/a/15195345/7190518
Upvotes: 0