3

function cross() {
  var dik = document.getElementsByName('r1c1')[0].parentNode;
  dik.style.color = "red";
}
<table>
  <tr>
    <td><input type="radio" name="r1c1" value="TRUE"></td>
    <td><input type="radio" name="r1c1" value="FALSE"></td>
  </tr>

  <tr>
    <td><input type="radio" name="r1c2" value="TRUE"></td>
    <td><input type="radio" name="r1c2" value="FALSE"></td>
  </tr>

  <input type="button" name="sbmt" value="CHECK" onclick="cross();">

</table>

When i click the button , I want to change color of td block to red that contains input element <input type="radio" name="r1c1" value="TRUE">

Bhargav Chudasama
  • 6,928
  • 5
  • 21
  • 39
Dhairya Lakhera
  • 4,445
  • 3
  • 35
  • 62

4 Answers4

5

You have to use property backgroundColor to change the color of td. color is used to change the color of the text

Read Here color vs backgroundColor

<script type="text/javascript">
function cross(){
    var dik = document.getElementsByName('r1c1')[0].parentNode;
    dik.style.backgroundColor = "red";
}
</script>

<table>
<tr>
    <td><input type="radio" name="r1c1" value="TRUE"></td>
    <td><input type="radio" name="r1c1" value="FALSE"></td>
</tr>

<tr>
    <td><input type="radio" name="r1c2" value="TRUE"></td>
    <td><input type="radio" name="r1c2" value="FALSE"></td>
</tr>

<input type="button" name="sbmt" value="CHECK" onclick="cross();">

</table>
Sanchit Patiyal
  • 4,910
  • 1
  • 14
  • 31
4

You need to use backgroundColor rather than color:

<script type="text/javascript">
  function cross() {
    var dik = document.getElementsByName('r1c1')[0].parentNode;
    dik.style.backgroundColor = "red";
  }
</script>

<table>
  <tr>
    <td><input type="radio" name="r1c1" value="TRUE"></td>
    <td><input type="radio" name="r1c1" value="FALSE"></td>
  </tr>

  <tr>
    <td><input type="radio" name="r1c2" value="TRUE"></td>
    <td><input type="radio" name="r1c2" value="FALSE"></td>
  </tr>

  <input type="button" name="sbmt" value="CHECK" onclick="cross();">

</table>
Pete
  • 57,112
  • 28
  • 117
  • 166
3

try with backgroundColor instead of color

color is change the text of color not background-color

dik.style.backgroundColor = "red";

function cross() {
  var dik = document.getElementsByName('r1c1')[0].parentNode;
  dik.style.backgroundColor = "red";
}
<table>
  <tr>
    <td><input type="radio" name="r1c1" value="TRUE"></td>
    <td><input type="radio" name="r1c1" value="FALSE"></td>
  </tr>

  <tr>
    <td><input type="radio" name="r1c2" value="TRUE"></td>
    <td><input type="radio" name="r1c2" value="FALSE"></td>
  </tr>

  <input type="button" name="sbmt" value="CHECK" onclick="cross();">

</table>
Bhargav Chudasama
  • 6,928
  • 5
  • 21
  • 39
1

You need to set the background color of the element to make it go red.

dik.style.background = "red";
cdx530
  • 128
  • 8