2
Well, I have 15 similar lines in my code and the only thing that changes is the id
of the elements that end in an increasing and different integer...
I tried to replace these lines using a loop, but every time I try to run the code nothing happens. I used the console.log
and the variable is normal. But still print an error message saying that style cannot be set on a 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 tbm replace the swith by:
for(i = 1; i < 5; i++) {
if("temporada_" + i == obj.id) {
document.getElementById('episodes_temp' + i).style.display="block";
break;
}
}
This happens because in fact you do not have in DOM any element with ids
'divAba' + i
, you can be sure, review this. Also, it would be appropriate to post the part of your HTML, after all we can not guess the rest of your code.– felipsmartins
@felipsmartins, in fact to the
getElementById
is a function, which takes as an argument astring
, and at the time of concatenation, the argument is returned with thei
, no problem. As you can see in this example: https://jsfiddle.net/SamirChaves/xcxj3skp/ . I think the problem is the rest of the code.– Samir Braga
@Samirbraga I understood what he is trying to do. And that is precisely why his question is missing details. The error message
Cannot read property
is precisely because ofgetElementById
returns null for not finding in DOM an element with the ID you are looking for. - By the way, I am also from Fortaleza.– felipsmartins
First person around here I see around here... rsrs... Yeah, I agree, waiting for new details Naine...
– Samir Braga
@Naine, where is this script? It may be being loaded before the element in question. Try to place it at the end of your document, so it will be loaded after all elements have been redeveloped.
– Samir Braga
i edited the post and put the link to the code I’m using I used the pq Pastebin I thought it got too big for here :( http://pastebin.com/bB26SDEK
– user37798