简体   繁体   中英

How to change font-weight for each 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 (so what I need the code to do is say for each keydown event the font-weight changes but not the whole text, so the first letter typed would have the smaller font-weight and stay that way but the following letters would grow in font-weight).

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

<button type="button" onclick="myFunction2()">uncensour</button>

<script>
   document.getElementById("testarea").onkeypress = function () { 
   myFunction() }
   document.getElementById("testarea").addEventListener("input", myFunction);
   var initWeight = 101;
   function myFunction() {
      document.getElementById("testarea").style.fontWeight = initWeight++ ;
   }
   function myFunction2() {
      document.getElementById("testarea").style.fontWeight = "101";
   }


   function myFunction2() {
      document.getElementById("testarea").style.fontWeight = "101";
   }
</script>

You will need to place each letter in a separate eg <span> tag, so that you can target each letter separately. Your output would end up looking something like this

<p><span class="bolder">h</span><span class="bold">e</span>llo</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