Click here to Skip to main content
15,886,101 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Script doesn't work. I want to make "tiposDeFrutas" visible with display attribute when I click over "nombreDeLista". I also add the CSS document. ............................................................................................................................................................................

<pre lang="HTML"><!DOCTYPE html>
<html>
<head>
<title>Pruebas HTML</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel=StyleSheet href="newcss.css" type="text/css" media=screen>
<script>
document.onload = function () {
document.getElementById("nombreDeLista").onclick = function () {
document.getElementsByClassName("tiposDeFrutas").style.display = "block";
};
};
</script>
</head>
<body>
<div id="nombreDeLista">Frutas</div>
<div class="tiposDeFrutas">Pera</div>
<div class="tiposDeFrutas">Limón</div>
</body>
</html>
____________________

.tiposDeFrutas {
display: none;
}


What I have tried:

................................................................................................................................................................................
Posted
Updated 10-Jan-18 8:20am

1 solution

1. use window.onload

2. getElementsByClassName returns an array, so you need to loop through that to get the elements

so...
JavaScript
window.onload = function () {
      document.getElementById("nombreDeLista").onclick = function () {
         var arr = document.getElementsByClassName("tiposDeFrutas");
         for (var i = 0; i < arr.length; i++) {
            arr[i].style.display = 'block';
         }
      }
   }
 
Share this answer
 
v4
Comments
Karthik_Mahalingam 10-Jan-18 22:10pm    
5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900