Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello,
My goal is: epvpImg: My paint skills are bad![^]

What I have tried:

I want multiple boxes with Read more / Read less function. I am able to create one working box but when adding more it doesn’t work. I am not that experienced.

What i have:
https://codepen.io/flo44781/pen/agZWwo -> One working Box with Read more / Read less

When i add new boxes do i need to to re write the Java Script for every new box i do?
Can i say the script that it only affect box-1 another script should only affect box-2 …

Best regards,
Florian
Posted
Updated 18-Jun-19 22:13pm

1 solution

just use
parentElement
and
childNodes
in javascript for dynamic event. In your button click event use
JavaScript
this
in your function or try the below answer. Use unique id for button

JavaScript
function myFunction(btn_id) {
  
  var btnText = document.getElementById(btn_id.id);
  var parentElem = btn_id.parentElement;
  var childElemP = parentElem.childNodes;
  var spanElem = childElemP[1].childNodes;
  
  if (spanElem[1].style.display === "none") {
    spanElem[1].style.display = "inline";
    btnText.innerHTML = "Read more"; 
    spanElem[2].style.display = "none";
  } else {
    spanElem[1].style.display = "none";
    btnText.innerHTML = "Read less"; 
    spanElem[2].style.display = "inline";
  }
}


I updated your codepen click here to see this.
[^]
 
Share this answer
 
v3

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