Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML DOM change style of Parent element

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">

like image 678
Curious Avatar asked Dec 12 '25 04:12

Curious


1 Answers

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>
like image 148
Sanchit Patiyal Avatar answered Dec 13 '25 16:12

Sanchit Patiyal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!