Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

205
Views
javascript display:none for each <span> components in a specified paragraph

I have a paragraph displaying over 10,000 lines of text and wanted to onclick remove all of them from displaying without hiding the paragraph they're displayed in (ie individually remoing each line of text by changing the span style="display:block" to "display:none"). I have other elements in the <p> that I don't want to be hidden, only the span's.

I can do them line by line, but that is not viable for the number of lines involved. Is there a quicker way to do this?

function uncheckAll() {
  document.getElementById("Box1").style.display = "none";
  document.getElementById("Box2").style.display = "none";
  document.getElementById("Box3").style.display = "none";
  document.getElementById("Box4").style.display = "none";
  document.getElementById("Box5").style.display = "none";

}
<body>
<p id="main" style="display:block" contenteditable="true">
<span id="Box1" style="display:block">text 1.</span>
<span id="Box2" style="display:block">text 2.</span>
<span id="Box3" style="display:block">text 3.</span>
<span id="Box4" style="display:block">text 4.</span>
<span id="Box5" style="display:block">etc.....</span>
</p>

<button style="height:50%;width:10%" onClick="uncheckAll()">Clear</button>
</body>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can try this

const uncheckAll = () => {
  var elements = document.querySelectorAll('#main span');

  for (var i = 0; i < elements.length; i++) {
    elements[i].style.display = "none";
  }
};
#main span {
  display: block;
}
<p id="main" style="display:block" contenteditable="true">
  <span id="Box1">text 1.</span>
  <span id="Box2">text 2.</span>
  <span id="Box3">text 3.</span>
  <span id="Box4">text 4.</span>
  <span id="Box5">etc.....</span>
</p>

<button style="height:50%;width:10%" onClick="uncheckAll()">Clear</button>

about 4 years ago · Juan Pablo Isaza Report

0

its easier

const uncheckAll = _ => {
  document.getElementById('#main').classList.add('hide-all');
};
.hide-all > span { display:none; }

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!