Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I want to give color to the table-header (th) after I click on radio button

this Is my code

https://codepen.io/mchedlo981/pen/ZErGPLN

<table>
    <tr>
        <th>
            <label for="1"><input id="1" type="radio">გამარჯობა</label>
        </th>
    <br>
        <th>
            <label for="2"><input id="2" type="radio">გამარჯობა</label> 
        </th>
    
    </tr>
    
</table>
    


th {
border: 1px solid;
width: fit-content;

}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

table {
display: flex;
justify-content: center
}

>Solution :

You have multiple ways to do this (you will need a little of Javascript).

I propose the next:

html file:


<table>
    <tr>
        <th class="th">
            <label for="1"><input id="rad1"  name ="1" type="radio">red</label>
        </th>
    <br>
        <th class="th">
            <label for="2"><input id="rad2" name ="1"  type="radio">green</label>   
        </th>
    
    </tr>
    
</table>

css file:

th {
    border: 1px solid;
    width: fit-content;
    
}

table {
    display: flex;
    justify-content: center
}

javascript file:

var th_list = document.getElementsByClassName("th");
document.getElementById("rad1").onclick = function() {
    for(var i = 0; i < th_list.length; i++) {
        th_list[i].style.setProperty("background", "red");
    }
};

document.getElementById("rad2").onclick = function() {
    for(var i = 0; i < th_list.length; i++) {
        th_list[i].style.setProperty("background", "green");
    }
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading