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

130
Views
Using JavaScript to highlight column based on dropdown list selection

I have a data table with a dropdown box that sorts the table by column. I am trying to highlight the column that is selected/sorted by the dropdown. I am using the following code to obtain the index of the dropdown item:

 <script>
 var sel = document.getElementById('asorting').selectedIndex;
 alert(sel);
 </script>

I am using the following CSS code to highlight the column:

 <style>
 table td:nth-of-type(3)
 {
 background-color:#E0E0E0;
 }
 </style>

Both of these work on their own, but I am trying to update the "table td:nth-of-type(3)" to change based on the value of my sel variable. I have tried using (" + sel + ") to feed the variable to the CSS, but that is not working.

I am not very experienced in JS and have not been able to find anything on this site that relates exactly to what I am trying.

Any help would be greatly appreciated.

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

0

You can toggle classes in javascript by arriving at a logic like this

<html>
    <head>
        <script>
            function changeStyle(v){
                elements = document.getElementsByClassName('hightlight');       
                if(elements.length > 0){
                    for (let element of elements){
                        element.classList.remove('hightlight');
                    }
                }       
                document.getElementById('data').children[parseInt(v)-1].className = "hightlight";
            }
        </script>
    </head>
    <body>
        <style>
            .hightlight {
                color : red
            }
        </style>
        <select id="asorting" onchange="changeStyle(this.value)">
            <option class="row" value="1">one</option>
            <option class="row" value="2">two</option>
            <option class="row"value="3">three</option>
        </select>
        <table>
            <tbody id="data">
                <tr>
                    <td>one</td>
                </tr>
                <tr>
                    <td>two</td>
                </tr>
                <tr>
                    <td>three</td>
                </tr>
            </tbody>
        </table>
    </body>
</html>

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!