1

I have many 'div's on a page with unique data attributes like:

<div class="map" data-buid="<?php echo $bUID; ?>" ></div>

How can I find all of such 'div's with jQuery so that jQuery functions use unique variables for separate 'div's? Something like:

bUID = $('div.map').data("buid");
init(bUID);
...
function init(bUID);

Thank you.

linuxoid
  • 1,415
  • 3
  • 14
  • 32
  • You mean do you want to get all divs with `data-buid` attribute? – Eddie May 10 '18 at 13:17
  • Your title and description appear to be asking two different things. If you want an array of unique values from the attribute (as the title says) use `map()` then use [one of the many ways](https://stackoverflow.com/questions/1960473/get-all-unique-values-in-an-array-remove-duplicates) to de-dupe it. If you want to select the elements that have the attribute (as the description says) Use the attribute selector: `$('[data-buid]')` – Rory McCrossan May 10 '18 at 13:18
  • bUID = $('div.map [data-buid]'); - this doesn't work – linuxoid May 10 '18 at 13:21

1 Answers1

1

you'll need to get them by the data tag and then loop through them

$.each($('[data-buid]'), function(i, elem){
    init($(elem).data('buid'));
})
Shahaf Antwarg
  • 489
  • 3
  • 11