Why comparison using javascript does not work?

1

What I want to do:

Algorithm that generates field change logs on the front end.

Problem:

Some of the code I compare to the old value with the new value:

            if( value =! anterior ){
                elemento.val( value ).attr( 'data-anterior', anterior ).toggleClass('log_inativo log');
            }else{
                elemento.toggleClass('log log_inativo');
            }

However, regardless of the values it is falling on the if and then on the infinitely infinite.

Code

JS

$(document).ready(function(){
//Grava o valor anterior do campo
    $( "input, select" ).focus(function(){            

        //Grava o atual valor em um atributo atributo
        var anterior = $(this.tagName + "[name="+ this.name +"][data-anterior]");           

        //Se o atributo não existe, ele é criado
        if(!anterior.length){
            $(this).attr('data-anterior', this.value);
        }
    }).change(function(){           

        //Define as variaveis
        var value = this.value;
        var anterior = $(this.tagName + "[name="+ this.name +"][data-anterior]").attr('data-anterior');
        var name = this.name;
        var elemento = $( "input[name=log_" + name + "]" );         

        console.log("value:", value, "anterior:", anterior);
        //Verifica se já não existe um elemento de log
        if( elemento.length ){

            //Verifica se o novo valor alterado não é o valor antigo
            if( value !== anterior ){
                elemento.val( value ).attr( 'data-anterior', anterior ).toggleClass('log_inativo log');
            }else{
                //Então remove
                elemento.toggleClass('log log_inativo');
            }
        }else{ //Se não existe ele cria
            $( ".formInner" ).append( "<input type = 'hidden' class = 'log' name = 'log_" + name + "' value = '" + value + "' data-anterior = '" + anterior + "'>" );
        }           

    })})

HTML

<body>
<div class = "formInner">
    <input type ="text" name = 'item1' value ="teste 01">
    <select name ='item2'>
        <option selected> banana </option>
        <option> banana 2 </option>
    </select>
</div>

Jsfiddle link

Watching

To do the check in line 20, from js, has console.log of the value entered and the previous value. Notethatclasshaschangedtolog_inativo,butthevalueenteredisdifferentfromthepreviousvalue

What am I doing wrong? Valeus.

    
asked by anonymous 26.11.2015 / 18:37

2 answers

1

My colleague Ivan solved my problem, which in this case was the toggle function. The comparison operation was working perfectly, but as the toggle function inverts the variables, regardless of the order that the class inserted inside it was placed, then it would always reverse regardless of the value of the value and previous variables.

Curious that I put this function to test, I was not going to actually use it. It replaces the remove function, since at the beginning of the algorithm and check if there is a hidden element with the name of the element, if not I create a new one.

//Verifica se o novo valor alterado não é o valor antigo
            if( value !== anterior ){
                elemento.val( value ).attr( 'data-anterior', anterior );
            }else{
                //Então remove
                elemento.remove();
            }
    
26.11.2015 / 19:10
0

The condition would be different values !==

Example:

var value = "a";
var anterior = "b";

if (value !== anterior) {
  document.write('diferente');
} else {
  document.write('igual');
}
    
26.11.2015 / 18:44