-1

The following script works perfectly, but when I try it with GetElementsByTagName it fails. An ID can only be used once and I want to use it for ('a', h1, h2).

Thoughts, ideas or solutions are welcome.

window.setInterval(function(){
    var t = document.getElementById('test');  
    var z = 'rgb('+ (Math.floor(Math.random() * 184)) + ',' 
                  + (Math.floor(Math.random() * 102)) + ',' 
                  + (Math.floor(Math.random() * 184)) + ')';                    
    t.style.color = z
}, 1000);
always-a-learner
  • 3,671
  • 10
  • 41
  • 81

1 Answers1

0

getElementById return only single element but getElementsByTagName return array. You can use this code.

window.setInterval(function(){
var t = document.getElementsByTagName('h1');  
var z = 'rgb(' + (Math.floor(Math.random() * 184)) + ',' 
                 + (Math.floor(Math.random() * 102)) + ',' 
                     + (Math.floor(Math.random() * 184)) + ')';
for(var i=0;i < t.length; i++)
{
  t[i].style.color = z
}

}, 1000);
Manoj Pilania
  • 664
  • 1
  • 7
  • 18