Aessandro
Aessandro

Reputation: 5761

jQuery function inside jQuery each not working as expected

The following code throws an error:

$(document).ready(function(){
    function assignColor(value, el){
        if(value < 10){
            el.style.background = "#CCC"
        } else if(value < 30){
            el.style.background = "#333"
        } else if(value < 100){
            el.style.background = "red"
        }
    };

    $('table .day_1 td').each(function() {
        console.log($(this));
        assignColor($(this).html(), $(this));
    });
});

I have a table which has 8 rows like the following:

        <tr class="day_1">
            <td class="first">Wed 5</td>
            <td>45</td>
            <td>01</td>
            <td>454</td>
            <td>03</td>
            <td>04</td>
            <td>22</td>
            <td>06</td>
            <td>07</td>
            <td>08</td>
            <td>09</td>
            <td>10</td>
            <td>11</td>
            <td>12</td>
            <td>13</td>
            <td>14</td>
            <td>15</td>
            <td>16</td>
            <td>23</td>
            <td>18</td>
            <td>19</td>
            <td>56</td>
            <td>21</td>
            <td>22</td>
            <td>23</td>
            <td>2564</td>
        </tr>

and based on the value that it's inside each td I want to assign a different bg color but I have the error specified above

Upvotes: 0

Views: 86

Answers (1)

Jonathan Rupp
Jonathan Rupp

Reputation: 15762

Try:

assignColor($(this).html(), this);

ie. the second arg to assignColor should be a DOM element, not a jQuery wrapper around a DOM element.

Upvotes: 4

Related Questions