html - show/hide button not working -


i creating php page shows mcq questions options , answers database. here using html button show/hide answers. working first question while clicking on other questions works first question only. kindly help

<script> function myfunction() { var x = document.getelementbyid('mydiv'); if (x.style.display === 'none') {     x.style.display = 'block'; } else {     x.style.display = 'none'; } } </script>   <?php         while($row = mysqli_fetch_array($result))         {             echo "<h4 style=font-weight:bold; text-align:justify>";             echo "q $row[slno]) $row[question]";             echo "</h4><br>";             echo "<ul class=list-group>";             echo "<li class=list-group-item>1) $row[option1]</li>";             echo "<li class=list-group-item>2) $row[option2]</li>";             echo "<li class=list-group-item>3) $row[option3]</li>";             echo "<li class=list-group-item>4) $row[option4]</li>";             echo "<li class=list-group-item>5) $row[option5]</li>";             echo "<div id=mydiv style=display:none><li class=list-group-item><b>correct option : ($row[correct_option])</b></div></li>";             echo "<li class=list-group-item><button onclick=myfunction()>view answer</button></li>";             echo "</ul>";             echo "<hr color=#ff0000 size=5 width=100%>";         }     ?> 

any suggestions...

try this..

 <script>     function myfunction(num) {     var x = document.getelementbyid('mydiv'+num);     if (x.style.display === 'none') {         x.style.display = 'block';     } else {         x.style.display = 'none';     }     }     </script>   <?php   $num=1;         while($row = mysqli_fetch_array($result))         {             echo "<h4 style=font-weight:bold; text-align:justify>";             echo "q $row[slno]) $row[question]";             echo "</h4><br>";             echo "<ul class=list-group>";             echo "<li class=list-group-item>1) $row[option1]</li>";             echo "<li class=list-group-item>2) $row[option2]</li>";             echo "<li class=list-group-item>3) $row[option3]</li>";             echo "<li class=list-group-item>4) $row[option4]</li>";             echo "<li class=list-group-item>5) $row[option5]</li>";             echo "<div id=mydiv".$num." style=display:none><li class=list-group-item><b>correct option : ($row[correct_option])</b></div></li>";             echo "<li class=list-group-item><button onclick=myfunction('".$num."')>view answer</button></li>";             echo "</ul>";             echo "<hr color=#ff0000 size=5 width=100%>";           $num++;         }      ?> 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -