How to change font size and color using JS only? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

How to change font size and color using JS only?

https://code.sololearn.com/W3ftikqMkuQc/?ref=app In this code I want to change the font-color and font-size of the characters being printed.

19th Jun 2018, 6:42 AM
Aanisha Bhattacharyya
Aanisha Bhattacharyya - avatar
8 Answers
+ 11
Since you can not directly change the style of a character the way you were told, this would be a solution: function Hacker() {      var nm = list.value;      var l = nm.length;      for(var a = 0; a <l; a ++)      {          var k = nm.charAt(a);          document.write('<span style = "color: red; font-size: 24px">' + k + '</ span> <br/>');      } }
19th Jun 2018, 7:11 AM
Mickel
Mickel - avatar
+ 7
let el = document.getElementsByTagName('p')[0]; el.style.color = '#fff'; el.style.fontSize = '16px'; That way you can do it. You can also obtain the element by its ID or its class, that depends on you.
19th Jun 2018, 6:52 AM
Mickel
Mickel - avatar
+ 6
Hello, Aanisha Bhattacharyya ! Please try this example. In this code, javascript looks for an element by id, and replaces styles var num = document.getElementById("id"); num.style.color = "red"; num.style.fontSize = "25px";
19th Jun 2018, 6:54 AM
Alexander Sokolov
Alexander Sokolov - avatar
+ 5
Mickel ,you really helped. This is my finished code and hope you like it. https://code.sololearn.com/W3ftikqMkuQc/?ref=app
19th Jun 2018, 3:11 PM
Aanisha Bhattacharyya
Aanisha Bhattacharyya - avatar
+ 5
Alexander Sokolov ,Peter Claver you all also helped.Please try my code ,my best till date. https://code.sololearn.com/W3ftikqMkuQc/?ref=app
20th Jun 2018, 12:18 PM
Aanisha Bhattacharyya
Aanisha Bhattacharyya - avatar
+ 3
document.getElementById('element id').style.color='blue';
19th Jun 2018, 6:48 AM
Peter Claver
Peter Claver - avatar
+ 2
Peter Claver ,Mickel ,Alexander Sokolov In this code I want to change the style and font-color of the characters being printed. https://code.sololearn.com/W3ftikqMkuQc/?ref=app
19th Jun 2018, 7:02 AM
Aanisha Bhattacharyya
Aanisha Bhattacharyya - avatar
+ 2
Mickel Thanks a lot.
19th Jun 2018, 7:14 AM
Aanisha Bhattacharyya
Aanisha Bhattacharyya - avatar