JSFiddle - React, Tailwind, and code Playground

by wirey00

HTML

<!-- Notice the structure of everything -->
<div id='container'>  <!-- AT DIV LEVEL -- This is the parent div of the table.. and ancestor div to everything inside the table -->
    <table> <!-- AT TABLE LEVEL -- one level up is the div.. one level down and you get the THEAD and TBODY -->
        <thead> <!-- AT THEAD LEVEL -- one level up is the table.. one level down and you get the tr -->
            <tr>  <!-- AT TR LEVEL -- one level up is the thead .. one level down and you get the th -->
                <th>Name</th>  <!-- AT TH LEVEL -- one level down and you get the tr and there are no levels below this -->
                <th>Age</th>  <!-- everything at the same level is a sibling -->
                <th>Sex</th>
                <th>Birthday</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Bob</td>
                <td>20</td>
                <td>Male</td>
                <td>07/25/1992</td>
            </tr>
            <tr>
                <td>Carol</td>
                <td>23</td>
                <td>Female</td>
                <td>05/25/1989</td>
            </tr>
            <tr>
                <td>Sue</td>
                <td>18</td>
                <td>Female</td>
                <td>03/16/1994</td>
            </tr>
            <tr>
                <td>Jim</td>
                <td>25</td>
                <td>Male</td>
                <td>05/25/1987</td>
            </tr>
            <tr>
                <td>Wayne</td>
                <td>30</td>
                <td>Male</td>
                <td>03/16/1982</td>
            </tr>
        </tbody>
    </table>
<div>
        
<button>Click ME</button>

CSS

#container th,#container td {
 border: 1px solid red;
 padding: 0px 5px 0px 5px;    
}

#container th {
 background-color: grey;
}

#container td {
 background-color: lightblue;
}

JavaScript

// This is an example of how you get Carol

// I'm binding click event to button which is generic and probably not a good idea
// but I know this is the only button on my page and was just lazy
$('button').click(function() {
    // inside here "the function" is what we want to happen when we click the button
    
    // So if we want to get to Carol.. and lets say change the background color
    // this selector says starting at element with id=container
    // find any descendant tables 
    // then find any descendant tbodys 
    // then find all td that contains "Carol"
    // Since this is a selector, we only get the final result(a jQuery Object/ or Objects)
    //$('#container table tbody td:contains(Carol)').css('background-color','yellow');
    
/***
 Comment above css code and uncomment below how the selectors work.
***/    
    // this will get all tr's under tbody -- Notice how it doesn't get the tr for the THEAD
    //$('#container table tbody tr').css('border','5px solid green')
        
    // this will get all tr's under thead
    //$('#container table thead tr').css('border','5px solid green')
     
    // We can also use direct child selectors - which will only get direct childs and nothing further down the DOM Structure
    //$('#container > table > tbody > tr') 
        
    // Traversing the DOM (traveling the DOM structure) is very important and can be done many ways
    // Here's another way you can get to Carol
    //$('#container table tbody tr').eq(1).children('td').eq(0).css('background-color','orange');
    
    // the .eq() method is how you get a specifig jQuery object out of the collection/array.. lets say in an array
    // you have three numbers 
    // var x = [0,2,4];
    // for arrays you get your element specifying the index which arrays are zero indexed.. so are jQuery collections
    // so if I want to get the number 2 i can get it like this since number 2 is in index 1
    // x[1]
    
    // jQuery is the same thing with...