jquery - Get data-attribute from element that's found with get() -


i feel missing , can't figure out what. try data attribute of item that's specified index through .get(). however, can't seem so:

var int = 1,  		selector = $("a");        console.log(selector.get(int));  console.log(selector.get(int).data("banana")); // uncaught typeerror: selector.get(...).data not function  console.log(selector.get(int)[0].data("banana")); // uncaught typeerror: selector.get(...).data not function
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <a href="#" data-banana="5">hello</a>  <a href="#" data-banana="2">there</a>

what missing here? why happen?

you have use dataset @ context,

console.log(selector.get(int)[0].dataset.banana); 

because node object not have method called data() in prototype. function belongs jquery object.

if want travel along jquery @ situation have use .eq()

console.log(selector.eq(0).data("banana")); 

basically .get(1) extract second element node object jquery collection, whereas .eq(1) second element jquery object


Comments