First page
and m...">

How to maintain data integrity?

I have this basic HTML:

<div id ="div1" data-pagenumber="0001">First page</div> <div id = "div2"></div> 

and my jQuery:

 var number = $('#div1').data('pagenumber') $('#div2').append(number); 

Fiddle: http://jsfiddle.net/JabUS/

Why is the text inside div2 set to 1 ? It seems to me that jQuery automatically changes my value. How to prevent this conversion so that it prints as follows: 0001 ?

+4
source share
1 answer

You need to use:

 var number = $('#div1').attr('data-pagenumber'); 

because from jQuery docs :

Each attempt is made to convert a string to a JavaScript value (this includes booleans, numbers, objects, arrays, and null); otherwise, it remains as a string. To get the value attribute as a string without any attempt to convert it, use the attr () method

Updated Fiddle

But in fact, everything should be fine if you are using jQuery version 1.8 +.

+5
source

Source: https://habr.com/ru/post/1479570/


All Articles