question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pass DOM element to selection functions

See original GitHub issue

Currently the context of a function passed to selection.attr (and its sibling operations) is set to the DOM element of the selection. Things get tricky when you want to both use the outer this and do something with the DOM element. Of course, a var self = this; before the function solves this.

var self = this;

d3.select('#some-element')
    // Completely contrived example of using both this and self:
    .attr('class', function() {
        return (self.highlightA && this.dataset.a) ? 'highlight' : 'normal';
    });

What would be more convenient is if the DOM element was available as a parameter to the function, i.e. (d, i, element). This would allow you to use ES6’s arrow functions, which ignore the function’s context.

d3.select('#some-element')
    // Completely contrived example of using both this and self:
    .attr('class', (d, i, elem) => (this.highlightA && elem.dataset.a) ? 'highlight' : 'normal');

To be clear, I’m not proposing that the context be changed to something else, only that it be possible to get the relevant DOM element without it.

Regarding backwards compatibility, adding an additional argument shouldn’t break anyone’s code, unless they’re for some reason relying on arguments being a particular length (though if my assumption is mistaken and such a change would indeed be a breaking one, it should probably be left until version 4).

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
jasondaviescommented, Mar 24, 2015

Related: ES6’s fat arrow syntax uses a lexical binding for this. In other words, you cannot access the this context that D3 passes to callbacks if you’re using the fat arrow syntax. The solution: don’t use the fat arrow syntax, or alternatively, we may need to consider something like the suggestion above for version 4.0.

2reactions
mbostockcommented, Mar 1, 2016

I’ve standardized d3-selection and d3-transition on the current datum d, the group index i, and the group nodes array. This is intended to mimic array methods such as array.forEach. The this is the current element.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Passing a DOM element as a function parameter in JavaScript
In this lesson, we will learn how to pass DOM elements as function parameters in JavaScript.
Read more >
Passing DOM element ID to JavaScript function - Stack Overflow
I have the following codes in my Django template, in this exact order and proximity: <div id="test-modal"> <p id="test-text">Display on ...
Read more >
Selection and Range - The Modern JavaScript Tutorial
JavaScript can access an existing selection, select/deselect DOM nodes as a whole or partially, remove the selected content from the document, ...
Read more >
Refs and the DOM - React
Refs provide a way to access DOM nodes or React elements created in the render ... and close() methods on a Dialog component,...
Read more >
Data Binding to DOM in D3 - TutorialsTeacher
D3 is data driven. The data() function is used to join the specified array of data to the selected DOM elements and return...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found