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

108
Views
Applying class tags to individual JavaScript Array entries after sorting

I am trying to apply the same css class to each indivdual entry in the created array, but instead I am getting it apllied once to the whole array.

My Code is as follows..

    function sortballhistory() {
        var str = document.getElementById("ballhistory").innerText;
    var temp = new Array();
    // This will return an array with strings "1", "2", etc.
    temp = str.split(",");
    temp.sort(function (a, b) {  return a - b;  });
    temp = 'temp.slice(1);
    document.getElementById('ballhistorysortedarr').innerHTML = temp;

}; 

ballhistory innerText contains shuffled numbers 1-90 like (2, 5, 7, 76, etc) these change each page load.

I have tried 1)

temp = '<span class="smallball">' + temp.slice(1) + '</span>';
document.getElementById('ballhistorysortedarr').innerHTML = temp;

and also tried 2)

  document.getElementById('ballhistorysortedarr').innerHTML = '<span class="smallball">' + temp; + '</span>';

and also 3)

 document.getElementById('ballhistorysortedarr').innerHTML += '<span class="smallball">' + temp; + '</span>';

But non of them give the desired result of styling each entry seperatly.

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

0

You need to iterate through each element in the temp array and return the element wrapped inside the span. That's how you can provide the class to the span, Please check the code below, and let me know if this is what you were trying to achieve.

var btn = document.getElementById("btn");

btn.addEventListener('click', function() {
  sortballhistory();
})

function sortballhistory() {
        var str = document.getElementById("ballhistory").innerText;
    var temp = new Array();
    // This will return an array with strings "1", "2", etc.
    temp = str.split(",");
    temp.sort(function (a, b) {  return a - b;  });
    var result = '';
    for(var i=0; i<temp.length; i++) {
      result+="<span class='item'>" + temp[i] + "</span>"
    }
    document.getElementById('ballhistorysortedarr').innerHTML = result;

}; 
.item {
  color: red;
}
<div id="ballhistory">2, 3, 5,4, 6, 2, 5, 1, 8, 4</div>
<button id='btn'>Click</button>

<div id='ballhistorysortedarr'></div>

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!