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

180
Views
getElementsByClassName plus a value filter of the data inside this element?

I need to add a filter to a function.

I have different elements in the page with the class a-text-center table-border, but I need the code working only if the element value is > 1, so other elements won't be affected by the script.

I can not add anything to the code, such as an ID or anything else to the element to differentiate it from the others, so the only way to differentiate it is using the value of the number inside this element.

Example of the element:

<td class="a-text-center table-border">4</td>

In this case the value is 4.

This is the script:

$(document).ready(function() {
  var x = (document).getElementsByClassName('a-text-center table-border');
  for (var i = 0; i < x.length; i++)
    x.item(i).className += " bordertopred";

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

Firstly, note that td elements do not have a value. What you're trying to read is the innerText of the element. This can be done using filter() after selecting the necessary elements from the DOM.

Also note that if you include jQuery in the page you may as well make use of it to do what you require. Below are two versions of the code, one utilising jQuery and the other plain JS.

First the plain JS:

document.addEventListener('DOMContentLoaded', () => {
  let valueToMatch = '4';
  let elements = Array.from(document.querySelectorAll('.a-text-center.table-border')).filter(el => el.innerText.trim() === valueToMatch);
  elements.forEach(el => el.classList.add("bordertopred"));
});
td {
  padding: 10px; 
}
.bordertopred {
  background-color: #CC0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<table>
  <tr>
    <td class="a-text-center table-border">1</td>
    <td class="a-text-center table-border">2</td>
    <td class="a-text-center table-border">3</td>
    <td class="a-text-center table-border">4</td>
  </tr>
</table>

This would be the jQuery equivalent:

jQuery($ => {
  let valueToMatch = '4';
  $('.a-text-center.table-border').filter((i, el) => el.innerText.trim() === valueToMatch).addClass('bordertopred');
});
td {
  padding: 10px; 
}
.bordertopred {
  background-color: #CC0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<table>
  <tr>
    <td class="a-text-center table-border">1</td>
    <td class="a-text-center table-border">2</td>
    <td class="a-text-center table-border">3</td>
    <td class="a-text-center table-border">4</td>
  </tr>
</table>

about 4 years ago · Santiago Gelvez Report

0

I not understand. You can't add if statement at your function?

<script>
$(document).ready(function(){
    var x = (document).getElementsByClassName('a-text-center table-border');
for(var i = 0; i < x.length; i++) {
   if (i > 1)
     x.item(i).className += " bordertopred";
}
</script>

Also i would be inclined to use x.item(i).className.add('yourclass');

about 4 years ago · Santiago Gelvez 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!