Javascript error: Can not read property 'length' of null [duplicated]

1

I'm making an audio player, and every time I upload the page, the background will change, for that I'm using Javascript, although I do not usually program with it.

<script language='JavaScript'>";
                var image = new Array();
                image[1]='Postal1.jpg';
                image[2]='Portal2.jpg';
                image[3]='portal3.jpg';
                image[4]='Portal4.jpg';
                image[5]='Postal5.jpg';
                image[6]='Portal6.jpg';
                image[7]='Postal7.jpg';
                image[8]='Postal8.jpg';
                image[9]='Portal9.jpg';
                image[10]='Postal10.jpg';
                var src= image[Math.floor(Math.random()*10) + 1];
                var fondomp3 = document.getElementsByClassName('fondoaudio');
                for(var i = 0; i <= fondomp3.length; i++){
                  var fondomp31 = document.getElementsByClassName('fondoaudio')[i];
                  fondomp31.style.backgroundImage = 'url('+src+')';
                }
</script>

the HTML:

<div class='fondoaudio'>
   <div id='player'>
      <div id='songTitle'>Song1.mp3</div>
      <div id='buttons'>
         <button id='play' class='play_1'><img src='play1.png'/></button>
         <button id='pause' class='pause_1'><img src='pause1.png'/></button>
      </div>
   </div>
</div>
<div class='fondoaudio'>
   <div id='player'>
      <div id='songTitle'>Song2.mp3</div>
      <div id='buttons'>
         <button id='play' class='play_2'><img src='play1.png'/></button>
         <button id='pause' class='pause_2'><img src='pause1.png'/></button>
      </div>
   </div>
</div>
<div class='fondoaudio'>
   <div id='player'>
      <div id='songTitle'>Song3.mp3</div>
      <div id='buttons'>
         <button id='play' class='play_3'><img src='play1.png'/></button>
         <button id='pause' class='pause_3'><img src='pause1.png'/></button>
      </div>
   </div>
</div>
<div class='fondoaudio'>
   <div id='player'>
      <div id='songTitle'>Song4.mp3</div>
      <div id='buttons'>
         <button id='play' class='play_4'><img src='play1.png'/></button>
         <button id='pause' class='pause_4'><img src='pause1.png'/></button>
      </div>
   </div>
</div>

the script what it does, is to show a random background, with all those images. Ok, this is fine, but I'm telling you to change the background to the label with the class' fondoaudio ', but the div's with whose class there are several, so, I just showed the background a div with the class' fondoaudio ', so that I put it in all the' fondoaudio ', I make a for loop, so that I insert the background in all, the for is going well, but the problem is that it shows me the images in the three div's and not in the four of them. According to Kko_L, it says that it is because document.getElementsByClassName('fondoaudio') gives me null. Well, in summary, theoretically this part of the script:

for(var i = 0; i <= fondomp3.length; i++){
  var fondomp31 = document.getElementsByClassName('fondoaudio');
  fondomp31.style.backgroundImage = 'url('+src+')';
}

And in the console I get this error:

Uncaught TypeError: Cannot read property 'style' of undefined

I would have to show the funds in the 4 div's show me in the 3 div's. And I do not know where the problem comes from and how to solve it.

Thank you.

    
asked by cat_12 25.11.2018 в 15:02
source

3 answers

1

This happens because "main" to be an ID that can only exist once, all others are probably discarded that you take the first main and it is to which the image is applied, to fix this it is best to use a class instead of an id since the classes are re-usable ie you can use them in more than 1 div

I would say this would be a good start.

var src= image[Math.floor(Math.random()*10) + 1];
var fondomp3 = document.getElementsByClassName("main");
for (var i = 0; i < fondomp3.length; i++) {
    fondomp3[i].style.backgroundImage = 'url('+src+')';
}
    
answered by 25.11.2018 в 15:20
1

Check that there is an HTML element with the ID="main" attribute. If there is no HTML element with that ID, fondomp3 will always be NULL .

    
answered by 26.11.2018 в 10:55
1

The problem is that you have a <= in for :

for(var i = 0; i <= fondomp3.length; i++){
  var fondomp31 = document.getElementsByClassName('fondoaudio');
  fondomp31.style.backgroundImage = 'url('+src+')';
}

fondomp3 has 4 elements but the array in javascript is z-based so the element fondomp3[4] does not exist. Hence, fondomp31 in that iteration is undefined

Change that <= by < and solved:

for(var i = 0; i < fondomp3.length; i++){
      var fondomp31 = document.getElementsByClassName('fondoaudio');
      fondomp31.style.backgroundImage = 'url('+src+')';
    }

I leave the code with those changes and some more:

let image = ['https://images.vexels.com/media/users/3/134987/isolated/lists/2f7ac9861a2050983694bb9c7f1859c1-centro-de-llamada-con-el-fondo.png', 
'https://images.vexels.com/media/users/3/135149/isolated/lists/f7da46129f048eafb4bb8d23b14b00af-se-al-de-mensaje-con-el-fondo.png',
'http://4.bp.blogspot.com/-AhxMERr_smw/T6SuJ3itbuI/AAAAAAAAQFg/jsa92CJkEFI/s1600/eye.png',
'https://www.aprendemarketingonline.com/wp-content/uploads/2017/05/facebook-circular-logo-icono-azul-18a1c7-fondo-transparente.png',
'https://images.vexels.com/media/users/3/152381/isolated/lists/eb07cb8fa6347f036a28a1ffb3ef77a0-fondo-de-ilustraci-n-de-confeti.png',
'https://pngimage.net/wp-content/uploads/2018/06/logo-instagram-sin-fondo-png-3.png',
'https://www.aprendemarketingonline.com/wp-content/uploads/2017/05/graduation-student-black-cap-icono-azul-18a1c7-fondo-transparente.png',
'https://2.bp.blogspot.com/-tDx8uPae-rk/T4fg0bkl2RI/AAAAAAAANPo/wvWKjSV4kqE/s1600/heart.png',
'https://www.ineaf.es/tribuna/wp-content/uploads/Fondo-de-Comercio-Tribuna-INEAF.png',
'https://www.aprendemarketingonline.com/wp-content/uploads/2017/05/graduation-student-black-cap-icono-azul-18a1c7-fondo-transparente.png']

let src= image[Math.floor(Math.random()*10) + 1];
let fondomp3 = document.getElementsByClassName('fondoaudio');
  for(let i = 0; i < fondomp3.length; i++){
    let fondomp31 = document.getElementsByClassName('fondoaudio')[i];
    fondomp31.style.backgroundImage = 'url('+src+')';
}
<div class='fondoaudio'>
   <div id='player'>
      <div id='songTitle'>Song1.mp3</div>
      <div id='buttons'>
         <button id='play' class='play_1'><img src='https://cdn3.iconfinder.com/data/icons/developperss/PNG/Play%20Green%20Button.png'/></button>
         <button id='pause' class='pause_1'><img src='https://cdn3.iconfinder.com/data/icons/developperss/PNG/Pause%20Green%20Button.png'/></button>
      </div>
   </div>
</div>
<div class='fondoaudio'>
   <div id='player'>
      <div id='songTitle'>Song2.mp3</div>
      <div id='buttons'>
         <button id='play' class='play_2'><img src='https://cdn3.iconfinder.com/data/icons/developperss/PNG/Play%20Green%20Button.png'/></button>
         <button id='pause' class='pause_2'><img src='https://cdn3.iconfinder.com/data/icons/developperss/PNG/Pause%20Green%20Button.png'/></button>
      </div>
   </div>
</div>
<div class='fondoaudio'>
   <div id='player'>
      <div id='songTitle'>Song3.mp3</div>
      <div id='buttons'>
         <button id='play' class='play_3'><img src='https://cdn3.iconfinder.com/data/icons/developperss/PNG/Play%20Green%20Button.png'/></button>
         <button id='pause' class='pause_3'><img src='https://cdn3.iconfinder.com/data/icons/developperss/PNG/Pause%20Green%20Button.png'/></button>
      </div>
   </div>
</div>
<div class='fondoaudio'>
   <div id='player'>
      <div id='songTitle'>Song4.mp3</div>
      <div id='buttons'>
         <button id='play' class='play_4'><img src='https://cdn3.iconfinder.com/data/icons/developperss/PNG/Play%20Green%20Button.png'/></button>
         <button id='pause' class='pause_4'><img src='https://cdn3.iconfinder.com/data/icons/developperss/PNG/Pause%20Green%20Button.png'/></button>
      </div>
   </div>
</div>
    
answered by 27.11.2018 в 13:38