简体   繁体   中英

How to change font-weight for every letter typed using javascript

I have this variable font that I made and I want to implement it on a website, where when the user types, the font-weight for each letter grows. I have this code right now that it grows for the whole text typed, but everything changes there, not each letter. The goal would be to have the font weight grow for each on change (so each letter typed) Thank you in advance if anyone can help out!


<p id="testarea" contentEditable="true">
Type your text here
</p>

<script>
document.getElementById("testarea").onkeypress = function () { myFunction() }
;
var initWeight = 101
var x = 2;
function myFunction() {
    document.getElementById("testarea").style.fontWeight = initWeight+=50  ;
}
</script>

As far as I know, font weights go by a 100, so 100-200-300 etc. I have improved your script and it should work now with proper font.

 document.getElementById("testarea").onkeypress = function () { myFunction() }; var initWeight = 100 function myFunction() { initWeight += 100; document.getElementById("testarea").style.fontWeight = initWeight; }
 <p id="testarea" contentEditable="true"> Type your text here </p>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM