44

This code is not working

var span = document.getElementById("span");
span.style.fontsize = "25px";
span.innerHTML = "String";


jams
  • 20,714
  • 26
  • 72
  • 94

5 Answers5

79

JavaScript is case sensitive.

So, if you want to change the font size, you have to go:

span.style.fontSize = "25px";
ambiguousmouse
  • 1,945
  • 2
  • 22
  • 27
  • 3
    Looks like it is VS2008's bug. Intellisense providing wrong, it is providing `fontsize` but it should provide `fontSize`. "s" character of word "fontSize" should be capital. – jams Apr 07 '11 at 20:06
  • In JavaScript Intellisense only works meh. It usually provides names, that have already been used so far. That is, if you have declared `fontsize` somewhere, you'll get `fontsize` listed. That has nothing to do with the `.style.fontSize`, however. – Otto Abnormalverbraucher Jan 09 '14 at 13:08
  • 2
    In CSS, though, the property is called `font-size'. Why does it have a different name to access the same property in javascript? – Elliot Apr 08 '15 at 18:05
  • 1
    In CSS font-size is the correct name but in javascript that would trasnlate to font minus style so everywhere in the CSS you have a dash in javascript the property would be camel cased without the dash. – Harry Chilinguerian Jul 29 '18 at 04:29
9
<span id="span">HOI</span>
<script>
   var span = document.getElementById("span");
   console.log(span);

   span.style.fontSize = "25px";
   span.innerHTML = "String";
</script>

You have two errors in your code:

  1. document.getElementById - This retrieves the element with an Id that is "span", you did not specify an id on the span-element.

  2. Capitals in Javascript - Also you forgot the capital of Size.

Iswanto San
  • 17,718
  • 12
  • 57
  • 79
Mark
  • 224
  • 1
  • 2
3

try this:

var span = document.getElementById("span");
span.style.fontSize = "25px";
span.innerHTML = "String";
Naftali
  • 142,114
  • 39
  • 237
  • 299
1

Please never do this in real projects:

document.getElementById("span").innerHTML = "String".fontsize(25);
<span id="span"></span>
tibalt
  • 14,163
  • 1
  • 28
  • 22
  • 1
    This is so awful I couldn't just pass along without upvoting. People need to know about this, specifically, not doing this! – Emile Bergeron Sep 13 '21 at 21:49
0
span.style.fontSize = "25px";

use this

Suraj Rao
  • 28,850
  • 10
  • 94
  • 99