Javascript getElementById from php foreach loop

advertisements

So I have a php foreach loop as follows,

$i = 1;
foreach($rows as $record){
       <div id='ytplayer".$i++."'>
       </div>");
}

and then I have javascript which uses the getElementById and it needs to get the div everytime it is looped, which would essentially be an array, but how would I do that, I know what I have below wont work, but something like this, since I know this is pretty close to how you would get this to work in php

document.getElementById('ytplayer[i]');


Here you go!

http://jsfiddle.net/epinapala/5yUAx/

Assuming that your PHP writes the following divs(one,two etc are added for example sake...)

<div id='ytplayer1'>one</div>
<div id='ytplayer2'>two</div>
<div id='ytplayer3'>three</div>
<div id='ytplayer4'>four</div>

for(var i=1; i<5; i++){
var x=document.getElementById('ytplayer'+i);
  alert(x.innerHTML);
}