change class label according to text

Asked

Viewed 507 times

1

Guys someone could help me with this js please?

I need the label to change according to the text, if it is "victory" is Success and is "defeat" is Danger, follow code below:

notes my ${resultad.statusResults} brings from the bank only "Win" and "Lose" and it prints this information on the screen, IE, really my js must 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>
    

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

  • Mark really your code is correct, I tested your example on the link, but when I put inside mine . jsp it does not run something must be conflicting that it does not work.

  • press F12 and go to console, and put what is appearing error here, then I can help better, go to the browser you are using

  • home:34 Uncaught Referenceerror: $ is not defined at home:34 (Anonymous) @ home:34 - ( and it points to this line of the script: ) - $(Document). ready(Function () {

  • check whether the Jquery file is being called, and whether it appears before the script because this error says that Jquery is not defined at the time the function is running

  • If this doesn’t resolve try to check the jQuery.noConflict()

  • i pasted jquery inside the <head>

  • yeah, it didn’t happen! I’ll keep trying here.

  • eheh, good luck at most, but check if the path of Jquery is correct, or try to use pure javascript would also be an option

  • Dude, "In order for it to work properly, in your HTML you must import jQuery first and then your script" that nonsense né hahahahaha solved!!! Vlw Marcos, thanks for your help too.

  • haha, good that you solved, kkkk because it is you have to reference before the library as I had said

  • if the reply helped you mark with

  • You spoke and I did not notice, you are correct! I wanted to give the solution in the text that you say it and not in the code above, as I do?

  • Just edit your reply and put below it the solution

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.