Selector jQuery eliminating zero incorrectly

2

In the following case, you have a code being reported in the data-produto attribute of my input.

$(".valor.partida").bind('blur', function(){
  alert($(this).data("produto"));
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script><inputclass="valor partida" type="text" name="novo_valor_partida-1" value="11,11" data-produto="0101000" data-id="1">

The problem is that when you get the value of this attribute, it returns without the initial zero, only 101000 .

Why does this occur?

    
asked by anonymous 19.05.2016 / 14:53

1 answer

5

Do not rely on jQuery .data() , it's not to be trusted :)

In this case the version 1.4 it interprets this string as number (!), but this has been fixed in later versions < a href="https://jsfiddle.net/jops79ou/1/"> and already returns string .

Use native JavaScript:

$(".valor.partida").bind('blur', function(){
     alert(this.dataset.produto);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script><inputclass="valor partida" type="text" name="novo_valor_partida-1" value="11,11" data-produto="0101000" data-id="1">

In a other answer I mentioned this problem, the @ bfavaretto also has an excellent explanation about internal processes of% of jQuery%.

    
19.05.2016 / 14:56