1
What I want to do:
Algorithm that generates field change logs in front-end.
Problem:
Certain code snippet I compare 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 into if and then into Else endlessly.
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 http://jsfiddle.net/anndreyy/gzdg3f5o/8/
Observation
To check on line 20, js, has console.log of the entered value and the previous value. 
 Note which class has changed to log_inactive, but the entered value is different from the previous value
Note which class has changed to log_inactive, but the entered value is different from the previous value

What am I doing wrong? Valeus.
=!should be!=. Will that be all?– bfavaretto
@bfavaretto so I analyzed the problem is just this. kk
– Gabriel Rodrigues
Really hadn’t touched me about it. how dumb I am =) However, the problem continues.
– Gustave Dupre
You are saying that the value is equal to the opposite of the previous (value =! value)
– Ivan Ferrer
And even switching, when you do the toggle, you reverse the condition. Making it never know which way you want to stay.
– Ivan Ferrer
Ivan, thank you. That was really the problem.
– Gustave Dupre
Another thing you can do is change:
.attr( 'data-anterior', anterior )for.data('anterior', anterior), when you use data-element, the word data is already an element reference, which will contain attributes.– Ivan Ferrer
Thanks, I’ll do it! Thanks.
– Gustave Dupre