change class label according to text

1

Can anyone help me with this js please?

I need the label to change according to the text, if it is "victory" it is success and for "defeat" it gets danger, it follows code below:

Note my $ {resultad.statusResults) brings the bank only "Victory" and "Defeat" and it prints this information on the screen, that is, really my js should be bad: /

  •   

    Script

    <script>
        $(document).ready(function () {
            $("span").each(function () {
                if ($(this).text() === "Vitória") {
                    //limpa class anterior
                    $(this).removeAttr('class');
                    //adiciona class desejada 
                    $(this).addClass('label label-success');
                } else if ($(this).text() === "Derrota") {
                    //limpa class anterior
                    $(this).removeAttr('class');
                    //adiciona class desejada 
                    $(this).addClass('label label-danger');
                }
            });
        });
    </script>
    
  •   

    html

                                    <table class="table">
                                        <thead>
                                            <tr>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <c:forEach items="${listaResultados}" var="resultad">
                                                <tr>
                                                    <td>${resultad.jogadorTemp}</td>
                                                    <td>${resultad.placar1}</td>
                                                    <td>${resultad.placar2}</td>
                                                    <td>${resultad.oponenteTemp}</td>
                                                    <td>${resultad.momento}</td>
                                                    <td><span class="label label-success">${resultad.statusResultados}</span></td>
                                                </tr>
                                            </c:forEach>
                                        </tbody>
                                    </table>
    
asked by anonymous 27.11.2017 / 19:03

1 answer

0

Search for $("tbody td") and check the value with $(this).html() instead of $(this).text() As the code below

$(document).ready(function () {
   var rt = $("tbody td").each(function () {
        if ($(this).html() === "Vitória") {
            //limpa class anterior
            $(this).removeAttr('class');
            //adiciona class desejada 
            $(this).addClass('label label-success');
        } else if ($(this).html() === "Derrota") {
            //limpa class anterior
            $(this).removeAttr('class');
            //adiciona class desejada 
            $(this).addClass('label label-danger');

        }
    });
    console.log(rt);
});

See an example working here

    
27.11.2017 / 19:16