0

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>

8
  • Alternatively, put a .hidden class with your <div>s, and remove/add this class when .solutionLink is clicked. Commented Nov 8, 2017 at 22:39
  • Why without jQuery? jQuery exists to make your life so much easier. Commented Nov 8, 2017 at 22:40
  • there's gotta be a duplicate for cases of getElementsByClassName('classname').propertythatonlyexistsonsingleelements Commented Nov 8, 2017 at 22:42
  • plus, getElementsByClassName wants a classname, not a selector. Commented Nov 8, 2017 at 22:42
  • there's a reason getElementsByClassName is named getElementsByClassName rather than getElementByClassName Commented Nov 8, 2017 at 22:43

3 Answers 3

1

var solutionLinks = document.querySelectorAll(".solutionLink");

for (var i = 0; i < solutionLinks.length; i++) {
  solutionLinks[i].addEventListener("click", function() {
    this.nextElementSibling.classList.toggle("hidden");
  });
}
.hidden {
  display:none;
}
<p>This problem #1</p>
<p class="solutionLink">Click here to see solution</p>
<div class="hidden">
  This is the solution to problem #1
</div>

<p>This problem #2</p>
<p class="solutionLink">Click here to see solution</p>
<div class="hidden">
  This is the solution to problem #2
</div>

Sign up to request clarification or add additional context in comments.

Comments

1

Your first problem is your css selector: The class solutions is really called solution.

Next I would recommend making use of the data-attributes. This way you can change your DOM-structure and don't have to change any js. Alternatively I would at least wrap every solution in its own container.

Below is a (in my opinion) nice implementation, but I still want to outline some problems with your js:

  • getElementsByClassName('.solution')̀ should begetElementsByClassName('solution')̀ , since the . is not part of the class name
  • getElementsByClassName returns elementS, as in more than one. You will have to access them using [0]

var solutionLink = document.querySelectorAll("p.solutionLink");

function openSolution(){
  document.getElementById(this.dataset.target).style.display = "block";
}

for(var i=0; i< solutionLink.length; i++){
  var solutions = solutionLink[i];
  solutions.addEventListener('click', openSolution, false);
}
.solution {display:none;}
<p>This problem #1</p>

<p class="solutionLink" data-target="sol1">Click here to see solution</p>

<div id="sol1" class="solution">
  This is the solution to problem #1
</div>

<p>This problem #2</p>

<p class="solutionLink" data-target="sol2">Click here to see solution</p>

<div id="sol2" class="solution">
  This is the solution to problem #2
</div>

Comments

0

Why are you currently attempting to retrieve the nextSibling of the very element that you wish to display (the textNode) ? Using the event Object target (the p.solutionLink), access the adjacent elementNode (div.solution) . According to your code the follow solution should work for you.

function openSolution(event){
  event.target.nextElementSibling.style.display = "block";
}

P.S why is your CSS selector plural .solutions ?

3 Comments

Thank you! I'm testing your code and gt this console error "Cannot read property 'nextElementSibling' of undefined"
Here is a working example. (with some added features) codepen.io/pablo-tavarez/pen/qVqGzX
Thanks a lot for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.