What is the difference in jQuery between prop and data? You can just prop ('blabla', value), why use data ()?

I will add: if you need to save any info to the element, then prop or data is better?

    2 answers 2

    .data allows you to get the date attribute value and cache it in an internal object. Cannot set attribute .

    .prop sets the value of a specific element property.

    If you just need to store some information, it is better to use data .

    • Once this caching got me great. An extra function on the background "it's time to retire for a long time, but we still want to explode" IMHO. - user207618 2:57
    • @Other and what about the problems? Maybe data () won't work for me either? What was the complexity of caching? - bsbak
    • @bsbak, it was not a matter of complexity, I just did not need caching, but it was. - user207618
     elem.data('property-name') аналогично elem.prop('data-property-name') 

    That's all the difference

    • not really similar - Grundy
    • @Grundy What is my mistake? - ilyaplot
    • one
      that data takes values ​​from the date of the attribute, but saves this value to its internal object. prop does not take values ​​from attributes, it takes values ​​from properties. Just add a snippet to your answer, demonstrating that these calls are really similar - Grundy