Compare a column’s value with the checkbox

Asked

Viewed 99 times

1

I’m trying to compare the value of a column of a table, so that depending on what is selected, show or hide the field, I arrived in this code:

            td = tr[i].getElementsByTagName("td")[3];
            console.log(td);
            debugger;
            if (td == $('#Produtos').prop("checked",true)) {
                    tr[i].style.display = "";
                } else {
                    tr[i].style.display = "none";
            }

But it is not filtering, how to correct? in the console.log(td) is showing up like this <td>True</td> depending on each line, am I taking wrong the value?

  • 1

    Try to do this, var tbBoolean = $(td).text() == "True" ? true : false; this code checks if the text inside td is equal "True", ai Voce can check the tdBoolean == $('#Produtos').prop("checked"). Detail: doing it here $('#Produtos').prop("checked",true) You are checking the checkbox

  • @Icaromartins when I do this, it returns me the following error: tdBoolean is not defined

  • 1

    You have to harvest this line first, right after the console.log. var tbBoolean = $(td).text() == "True" ? true : false;

  • I did it, it returned error on this line if (tbBoolean == $('#Produtos').prop("checked")) {

  • 1

    I made a mistake tdBoolean and then tbBoolean, and just turn everyone to tdBoolean

  • You gave it right, if you want to release the answer, I’ll mark it for you, thank you. @Icaromartins

Show 1 more comment

2 answers

1


After talking through the comments of the question we managed to reach the following solution =)

// Criar uma var boolean
var tdBoolean = $(td).text() == "True" ? true : false;

// modificar a checagem para 
if( tdBoolean == $('#Produtos').prop("checked") )
// if (td == $('#Produtos').prop("checked",true))
// isso porque o $('#Produtos').prop("checked",true)
// estava marcando o checkbox

1

            td = tr[i].getElementsByTagName("td")[3];
            console.log(td);
            debugger;

            var valorTd = (td == "True" ? true : false);

            if (valorTd  == $('#Produtos').is(":checked")) {
                    tr[i].style.display = "";
                } else {
                    tr[i].style.display = "none";
            }
  • Did not solve. It does not compare the values

Browser other questions tagged

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