​ ...">

Extract multiple data from DOM in jQuery?

There is a node like this

<img src=​"http:​/​/​x.JPG" data-latitude=​"0" data-longitude=​"0">​

      

In jQuery, I can extract two data attributes like this:

a=node.data("latitude")
b=node.data("longitude")

      

I was wondering if there is a way to extract multiple attributes data

in one go, for example:

latLng = node.data([latitude, longitude]) // not working

      

+3


source to share


3 answers


latLng = node.data();

      

this return object



{
   latitude:0,
   longitude:0
}

      

+6


source


I was wondering if there is a way to extract multiple attribute data in one go, for example:

latLng = node.data([latitude, longitude]) // not working

Note. Not sure if the expected result is an array of values ​​from .node.data()

?



var data = $.map($("img").data(), function(value) {
  return value
});
console.log(data);
      

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<img src=​"http:././.x.JPG" data-latitude="0" data-longitude="0">
      

Run codeHide result


+1


source


DEMO

var latLng = $('img').data();
$.each(latLng,function(index,value){
    alert(value); 
});

      

0


source







All Articles