How to get the data-id attribute?

后端 未结 15 2440
青春惊慌失措
青春惊慌失措 2020-11-21 23:29

I\'m using the jQuery quicksand plugin. I need to get the data-id of the clicked item and pass it to a webservice. How do I get the data-id attribute? I\'m using the .

15条回答
  •  你的背包
    2020-11-21 23:48

    If we want to retrieve or update these attributes using existing, native JavaScript, then we can do so using the getAttribute and setAttribute methods as shown below:

    Through JavaScript

    Through jQuery

    // Fetching data
    var fruitCount = $(this).data('fruit');
    OR 
    // If you updated the value, you will need to use below code to fetch new value 
    // otherwise above gives the old value which is intially set.
    // And also above does not work in ***Firefox***, so use below code to fetch value
    var fruitCount = $(this).attr('data-fruit');
    
    // Assigning data
    $(this).attr('data-fruit','7');
    

    Read this documentation

提交回复
热议问题