Well, I have 15 similar lines in my code, and the only thing that changes is id
of elements that end in an increasing integer and different from the others ...
I tried to replace these lines using a loop , but every time I try to run the code nothing happens. I used console.log
and the variable is normal. But anyway, print an error saying that style can not be set to null element: Uncaught TypeError: Cannot read property 'style' of null
'
<script>
function showSeason(obj) {
/*document.getElementById('episodes_temp1').style.display="none";
document.getElementById('episodes_temp2').style.display="none";
document.getElementById('episodes_temp3').style.display="none";
document.getElementById('episodes_temp4').style.display="none";*/
for(i = 1; i < 5; i++) {
document.getElementById('episodes_temp' + i).style.display="none";
}
//essa parte eu tbm queria por em um loop, mas nao tenho ideia de como fazer
//como serao diversos itens criar 50 cases nao seria uma boa escolha :(
switch (obj.id) {
case 'temporada_1':
document.getElementById('episodes_temp1').style.display="block";
break
case 'temporada_2':
document.getElementById('episodes_temp2').style.display="block";
break
case 'temporada_3':
document.getElementById('episodes_temp3').style.display="block";
break
case 'temporada_4':
document.getElementById('episodes_temp4').style.display="block";
break
}
}
</script>
</head>
<body>
<?php
echo "<p></p>";
for($i = 1; $i < 5; $i++) {
echo "<button type='button' id='temporada_$i' onclick='showSeason(this)'> Temporada $i </button>";
}
for($x = 1; $x < 5; $x++) {
echo "<div id='episodes_temp$x' style='display:none;'>";
for($i = 1; $i < 50; $i++) { //50 é um valor de teste, pretendo puxar do banco da dados;
echo "item $i";
}
echo "</div>";
}
?>
</body>
' I think I could also replace the swith by:
for(i = 1; i < 5; i++) {
if("temporada_" + i == obj.id) {
document.getElementById('episodes_temp' + i).style.display="block";
break;
}
}