-2
<table id="results">
            <thead>
            <tr>
                <td>1st</td>
                <td class="horse1"></td>
            </tr>
             <tr>
                <td>1st</td>
                <td class="horse2"></td>
            </tr>
             <tr>
                <td>1st</td>
                <td class="horse3"></td>
            </tr>
            </thead>
        </table>

i used this way it didnt worked . document.getElementById("result").className=horseId;

horseId is an variable and contains value

and there are multiple classes insede so how do i select a specific class to change.

Shiva Khattri
  • 13
  • 1
  • 7
  • 1
    You forgot quotes for your . classname "horseId" – Suresh Atta Jul 21 '17 at 03:15
  • 1
    you are also selecting `"result"` when it should be `"results"`. – demogorgon Jul 21 '17 at 03:16
  • your caption said you are trying it set it on a td which is several layers down from the object that you have the id on – Bindrid Jul 21 '17 at 03:17
  • 1
    The minimum text requirement is designed to get you to **actually state the problem clearly**, and dumping noise in to get past it is extremely disrespectful to this site and the people you're asking for **free help** to solve **your problem**. If you're not willing to follow the site guidelines, you should ask your questions at a different site. *I used this way it didn't work* is not a problem statement. – Ken White Jul 21 '17 at 03:23
  • Possible duplicate of [Change an element's class with JavaScript](https://stackoverflow.com/questions/195951/change-an-elements-class-with-javascript) – Clonkex Jul 21 '17 at 06:01

2 Answers2

0

You missed two things; first, you attempted to target by ID result, when you need results. Second, you need to wrap the desired class names in quotes. You can use a variable for the assignment, assuming the variable maps to a string.

It's also worth mentioning that your existing class horse1 is several nodes down from your target ID results. I assume this is the element you're trying to change.

To change the class of the #results element, you can use document.getElementById('results').className = horseId;.

To change the class of the .horse1 element, you can use document.getElementsByClassName('horse1').className = horseId;.

Here's an example of the latter:

var horseId = 'rainbow_dash';
document.getElementsByClassName('horse1').className = horseId;
console.log(document.getElementsByClassName('horse1').className);
<table id="results">
  <thead>
    <tr>
      <td>1st</td>
      <td class="horse1"></td>
    </tr>
  </thead>
</table>

Hope this helps! :)

Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
-2

It should be "results"

document.getElementById("results").className=horseId;
Whlcly
  • 11
  • 2