0

I am following this tutorial: http://alignedleft.com/tutorials/d3/adding-elements and am trying to get my hands dirty with d3 js.

I now created an index file with the following:

<!DOCTYPE html>
<html>
 <head>
  <script type="text/javascript" src="d3/d3.js">

   d3.select("body").append("p").text("New paragraph!");

  </script>
 </head>
 <body>
  <p>Hello!</p>
 </body>
</html>

This is located in a folder called d3 with all the contents of the file I downloaded here: https://d3js.org/

Now when I run the page I see "Hello!" but I do not see "New paragraph". So I assume the d3 part is not working. Any thoughts on what goes wrong here?

Frits Verstraten
  • 1,539
  • 5
  • 18
  • 32
  • @Frits I just wrote an answer but deleted it after [altocumulus](http://stackoverflow.com/questions/41446182/d3-js-code-is-not-working-in-basic-index-script#comment70098696_41446182) found the duplicate. However, I'd like to copy its last paragraph: "You can put a script in the or in the . But, as a good practice, code that manipulates the DOM should be at the end of the body." – Gerardo Furtado Jan 03 '17 at 15:02
  • @GerardoFurtado. Thanks but when I replace the script code in my body tags I still get dont see the "new paragraph" text. Any thoughts where this goes wrong – Frits Verstraten Jan 03 '17 at 15:15
  • How do you load the page? You will need to serve it from a web server; directly accessing the page from the filesystem won't work. Also see: [*"D3.js loading local data file from file:///"*](/q/15417437). – altocumulus Jan 03 '17 at 15:20
  • For a working demo on where to put the ` – altocumulus Jan 03 '17 at 15:26

0 Answers0