I'm working a very simple program that show the solution of the problem when the user click on the solution link. I'm trying to do this without using jQuery.
So the first thing that I did was to add a click to my p. But now I'm lost and I don't know how to display the solution.
Any ideas on how to fix this problem?
var solutionLink = document.querySelectorAll("p.solutionLink");
function openSolution(){
document.getElementsByClassName('.solution').nextSibling.style.display = "block";
}
for(var i=0; i< solutionLink.length; i++){
var solutions = solutionLink[i];
solutions.addEventListener('click', openSolution, false);
}
.solutions {display:none;}
<p>This problem #1</p>
<p class="solutionLink">Click here to see solution</p>
<div class="solutions">
This is the solution to problem #1
</div>
<p>This problem #2</p>
<p class="solutionLink">Click here to see solution</p>
<div class="solutions">
This is the solution to problem #2
</div>
.hiddenclass with your<div>s, and remove/add this class when.solutionLinkis clicked.getElementsByClassName('classname').propertythatonlyexistsonsingleelementsgetElementsByClassNamewants a classname, not a selector.getElementsByClassNameis namedgetElementsByClassNamerather thangetElementByClassName