1

All:

I wonder how do I select a DIV element with id="1" in D3, I can do it in jQuery, but when I turn to D3, it gives error like:

Uncaught DOMException: Failed to execute 'querySelector' on 'Document': 'div.chart#1' is not a valid selector

<div id="1"></div>

Thanks

Kuan
  • 10,085
  • 18
  • 76
  • 168

2 Answers2

7

The id TAG must start with letter, but you can use this:

d3.select('[id="1"]').append("div");

This is the example in JSFiddle

And here the technical explanation.

Baro
  • 3,875
  • 2
  • 12
  • 31
-1

If you gave it an ID, you can select it directly by ID. You don't need to specify div in the selector.

d3.select('#1')
.foo()
  • doesn't work for me either. I can select first object by type (in my case 'select') but not by id. EDIT: it works when I prefix the id with # – James Apr 08 '18 at 06:09