Search code examples
javascriptjqueryselecthtml-tablerow

Highlighting a row of table cells using a drop down select menu (different situation)


I'm a beginner so I don't really understand Javascript/jQuery too well.

I have a table with a drop down menu in the first cell.

What I want is to select an item from the drop down menu and highlight the corresponding row. So if I select 3, the ("3") row will be highlighted.

Similar to JSFiddle1:

$(document).ready(function() {
    var color = ['none', 'green', 'yellow', 'red'];
    $('table.table1').on('change','select', function() {
        $(this).parents('tr').css('background', color[$(':selected', this).index()]);
    });​
});

and JSFiddle2:

function myJSFunction(element)
{
    var row = element.parentNode.parentNode;

    switch(element.options[element.selectedIndex].innerHTML)
    {
        case "Pending":
            row.style.background = "#FF7E00";
            break;
        case "Approved":
            row.style.background = "green";
            break;
        case "Disapproved":
            row.style.background = "red";
            break;
        default:
            row.style.background = "white";
    }
}

This is the HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<link rel="stylesheet" href="style.css" />

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script>

<script src="script.js"> </script>

</head>

<body>

<div id="wrapper">

<table class="table1">

<tr>

<td><select name="tasks" id="tasks">
        <option value="">Tasks</option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
                <option value="6">6</option>
                <option value="7">7</option>
                <option value="8">8</option>
                <option value="9">9</option>
                <option value="10">10</option>

</select>   
</td>
<td class="days">Monday</td>
<td class="days">Tuesday</td>
<td class="days">Wednesday</td>
<td class="days">Thursday</td>
<td class="days">Friday</td>
<td class="days">Saturday</td>
<td class="days">Sunday</td>
</tr>

<tr class="row">
<td> 1 </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>

<tr class="row">
<td> 2 </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>

<tr class="row">
<td> 3 </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>

<tr class="row">
<td> 4 </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>

<tr class="row">
<td> 5 </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>

<tr class="row">
<td> 6 </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>

<tr class="row">
<td> 7 </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>

<tr class="row">
<td> 8 </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>

<tr class="row">
<td> 9 </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>

<tr class="row">
<td> 10 </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>

</table>

</div>

</body>

</html>

Solution

  • The following JSFiddle demonstrates the annotated code: http://jsfiddle.net/eQNp5/5/

    $("#tasks").change(function () {
        // Convert 1-based index to 0 based index
        var index = this.value - 1;
    
        // Select all rows in the table
        var $rows = $('.row', '.table1');
    
        // Set all rows to no background
        $rows.css('background-color', '');
    
        // Change the selected row's color to one
        $('.row', '.table1').eq(index).css('background-color', 'yellow');
    });