I'm trying to extract the FIRST data attribute from the following mark-up:
<button class="buttonMiddle" data-vid="1">
<a href="#">who helped them</a>
</button>
<button class="buttonMiddle" data-vid="2">
<a href="#">who helped them</a>
</button>
the JS I've conjured is:
var button1 = $('.buttonMiddle').get(0).data('vid');
console.log(button1);
Chrome's console gives me this:
Uncaught TypeError: Object # has no method 'data'
I've covered the basic troubleshooting --> jQuery is loaded before this js file, they are both loaded just before the closing body tag (so no need for $(function() {// find data-attribute});.
.get()will get you a DOM Element - if you want to use.data()you need a jQuery object because.data()is a jQuery method so using.eq()will return a jQuery object