Question

I'd like to set the variable player to the value "Leno Morales", which is also a value in my HTML table.

How do I concatenate the table value to my javascript?

Also, if I would like to change the player value based on the table value, how would I go about doing this?

At the moment, I have:

<tr onmouseover="this.style.backgroundColor='#ffff66';" 
    onmouseout="this.style.backgroundColor='#d4e3e5';" 
    onclick="location.href='#individualwellness'">
    <td>
        <script>
            var player = Leno Morales
        </script>
    </td>
    <td>
        RA
    </td>
    <td>
        Injured
    </td>
</tr>
Was it helpful?

Solution

<td id="player">
    <script>
            var player = "Leno Morales";
            document.getElementById("player").innerHTML = player;
    </script> 
</td>

But, The best thing is to put the script in the end of HTML to make sure the table is already rendered by browser.

OTHER TIPS

Add an id to the <td> tag where player name is to be inserted;

<tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';" onclick="location.href='#individualwellness'">
  <td id="playerValue">

  </td>
   <td>
     RA
  </td>
  <td>
     Injured
   </td>

Then you can set the value in tag by:

<script>
     var player = Leno Morales;
     document.getElementById("playerValue") = player;
</script>

Or you can add value by counting <td> tag:

<script>
         var player = Leno Morales;
         document.getElementByTagNames('td')[0] = player;
</script>

Just add an ID for the content form where you want to fetch the value like

<td id="playerValue">
    Leno Morales
</td>

Then to get the value inside a variable use the bellow code

var playerName=document.getElementById("playerValue").innerHTML;

Synopsis

We use the first ID attribute in the TD tag for making it unique for easily locating. Then we use innerHTML function to bring the inner HTML text (i.e. Leno Morales) and then in the next code we use the value to place in a global variable (i.e. playerName). The variable have be previously declared to use it as globally or for locally it can the declared in the function as here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top