The second argument of .data()
is a function that is executed for each data element and each DOM element in myselection
. It is passed the data and assumed to return whatever allows to match data and DOM elements. In your case, this would be .name
.
Your example doesn't have any data bound to the elements, so this doesn't work. You can however leverage this fact and check whether there is data bound and if not, return the ID of the DOM element:
provinces.data(data, function(d) { return d ? d.name : this.getAttribute("id"); });
Note that after this operation, the DOM elements will have data bound to them and the key function should work as expected. Complete demo here.