1

I am very new in learning JavaScript and html and CSS, I am trying to change the size of the all paragraphs in the document object here is my code but I am not getting any change and I noticed that documnet.getElementsByTagName('p') returns an empty object

 <html>
  <head>
    <script> 
       window.onload=function(){
       var paragraphs = document.getElementsByTagName("p");
       for(var i = 0; i < paragraphs.length; i++){
        paragraphs[i].style.fontSize = '45px';      
       } 
       }
    </script>
  </head>
  <body> 
     <p>helooop</p>
     <p>helooop</p> 
     <p>helooop</p>
     <p>helooop</p> 
  </body>

I don't know where went wrong . Could anyone helps please

zzlalani
  • 19,534
  • 16
  • 41
  • 72
user2730833
  • 175
  • 1
  • 2
  • 12

4 Answers4

1
      window.onload = function () {    
      var paragraphs = document.getElementsByTagName("p");    
      for (var i = 0; i < paragraphs.length; i++) {    
      paragraphs[i].style.fontSize = '3em';    
       }
      }

since you are increasing fontsize onload(which does not make sense to me),why not try this in your css itself:

* {
    font - size: 3em!important;
    color: #000 !important;
   font-family: Arial !important;
   }
HIRA THAKUR
  • 15,044
  • 14
  • 47
  • 82
0

Use $(document).ready() wrap (or this solution to avoid jQuery $(document).ready equivalent without jQuery)

You code works fine, but script fires before DOM is ready and any «p» is there

UPD

$(document).ready(function(){
      var paragraphs = document.getElementsByTagName("p");
     for(var i = 0; i < paragraphs.length; i++){
       paragraphs[i].style.fontSize = '45px';      
      } 
};

upd2

see coments, sorry =)

Community
  • 1
  • 1
paka
  • 1,521
  • 21
  • 35
0

The code is working fine

var paragraphs = document.getElementsByTagName("p");    
for (var i = 0; i < paragraphs.length; i++) {    paragraphs[i].style.fontSize = '45px';    
}

http://jsfiddle.net/4zvAP/

Sridhar R
  • 19,414
  • 6
  • 36
  • 35
0

Yes, code is working but it seems that there is some hidden character in the line with for() command which prevents proper parsing. I cut&paste the code to Chrome and it reported:

Uncaught SyntaxError: Unexpected token ILLEGAL 

I had to strip everything in front and after for() command to make it work.

Actually, lines 5 - 8 contain mix of 0020 and 3000 characters in front of text. Because of that both Firefox and Chrome reports SyntaxError.

Somehow you managed to put those chars in. You have to delete those chars in front of text and put there just spaces (or tabs). And your code will work as such.

Anto Jurković
  • 10,842
  • 2
  • 24
  • 41