← All examples

JavaScript Reading & focus stackoverflow.com

Stack Overflow: collapse low-score answers

Collapses answers below a score threshold into a bar that expands the full content on click; skips the accepted one.

Code to copy

/* Collapse answers below a score threshold, click to expand */
(function () {
  var MIN = 1;
  function collapse() {
    document.querySelectorAll('#answers .answer').forEach(function (a) {
      if (a.dataset.sofLow || a.classList.contains('accepted-answer')) return;
      var el = a.querySelector('.js-vote-count');
      if (!el) return;
      var score = parseInt(el.getAttribute('data-value') || el.textContent, 10);
      if (isNaN(score) || score >= MIN) return;
      a.dataset.sofLow = '1';
      var body = a.querySelector('.answercell');
      if (!body) return;
      body.style.display = 'none';
      var bar = document.createElement('div');
      bar.textContent = 'Low-score answer (' + score + ') — click to expand';
      bar.style.cssText = 'cursor:pointer;background:#f1f2f3;padding:6px 10px;border-radius:4px;font-size:13px;color:#555;';
      bar.addEventListener('click', function () {
        body.style.display = '';
        bar.remove();
      });
      a.insertBefore(bar, body);
    });
  }
  collapse();
  new MutationObserver(collapse).observe(document.body, { childList: true, subtree: true });
})();

How to use this example

  1. Copy the code with the button above.
  2. Install JustZix (2 minutes) and open the extension on the target page.
  3. Add a new rule matching that page.
  4. Paste the code into the rule's JavaScript panel and save — it runs on every page visit.

Rate this example

No ratings yet — be the first.

Does this example work?

Snippets are useless without somewhere to paste them.

JustZix takes 2 minutes to install and runs your code on every matching page. No account, no payment.

Download free See use cases