particles.js icon indicating copy to clipboard operation
particles.js copied to clipboard

Error in Console (Chrome)

Open derjulian opened this issue 7 years ago • 26 comments

The Console shows some error.

"particles.js:1495 Uncaught TypeError: Cannot read property 'getElementsByClassName' of null"

derjulian avatar Aug 04 '16 14:08 derjulian

I get the same error, did you manage to fix it?

elenadobrescu21 avatar Aug 12 '16 19:08 elenadobrescu21

Try use id, instead of class. I solve this problem exactly.

Xaz16 avatar Sep 12 '16 13:09 Xaz16

Try moving the script to the end of the Body, just before the footer

nchgn avatar Sep 12 '16 15:09 nchgn

I'm getting this same error.

cterrazas2 avatar Oct 05 '16 01:10 cterrazas2

So I had the <div id="particles-js"></div> in a partial, my app is an Angular SPA. Everything was loaded, (particles.js, particles.json, etc.) and then I would get this error.

However when I moved the <div id="particles-js"></div> to the index.html where the script was at then it loaded and worked.

cterrazas2 avatar Oct 05 '16 01:10 cterrazas2

Here is how to solve the problem in Foundation 6 if you happen use Zurb's build tools. You have to configure the JS across several files in the project. Here is a really useful Stack Overflow post: How to add JavaScript to Just One Page

xxalexdevxx avatar Dec 08 '16 15:12 xxalexdevxx

I had faced the same issue when trying to use particles.js in Angular 4.0.

  1. Initially I was loading the particles.js script in index.html; starter script particlesJS.load('particles-js', 'assets/particles.json', function() { console.log('callback - particles.js config loaded'); }); in index.html file and <div id="particles-js"></div> in app component html The app worked fine when using ng serve but when I was building the angular project using ng build and serving it through my express server, I couldn't see the animations and was getting an error in the console: Uncaught TypeError: Cannot read property 'getElementsByClassName' of null".

Then I fixed the bug using solution suggested by @cterrazas2 and it worked.

  1. After reading http://bit.ly/2ni9vvB on how to import external libraries in angular components, I loaded the particles.js script in angular CLI config and loaded starter script when the component is initialized(ngOnInit). Also shifting <div id="particles-js"></div> back in app component now worked.

Hope this helps.

piyushmakhija avatar Mar 25 '17 19:03 piyushmakhija

I had this same issue. It appears that we need to have <div id="particles-js"></div> in the html when we are loading particles js. Otherwise, it will throw an error.

miradnan avatar Jul 10 '17 08:07 miradnan

I got the same error. particles.js:1495 Uncaught TypeError: Cannot read property 'getElementsByClassName' of null

I use Angular 2. When I put <div id="particles-js"></div> in partial html, I'd got above error. But I moved it into index.html, there was no error. Instead of it, it(particles-js) didn't work for me.

In this case, how can I manage this?

ghost avatar Aug 12 '17 07:08 ghost

@piyushmakhija So how did you run it in a component with Angular 2?

ghost avatar Aug 12 '17 07:08 ghost

I am getting same error

Uncaught TypeError: Cannot read property 'getElementsByClassName' of null at window.particlesJS (scripts.bundle.js:1496) at scripts.bundle.js:1557

ahirhasmukh avatar Oct 11 '17 12:10 ahirhasmukh

I'm facing the same problem with WordPress theme. Can anyone give us the solution, please?

mdjwel avatar Dec 17 '17 15:12 mdjwel

Seems like particle.js is not compatible for React. Try to check this reactified particle.js: https://www.npmjs.com/package/react-particles-js

KBPsystem777 avatar Apr 27 '18 15:04 KBPsystem777

@naxogon suggestion worked for me, i believe it's because the particleJS() needs the element specified in it to be present(created) already. If we introduce the script above that element then first the script will be executed and error will occur as it won't find the target element for output. Correct me if i'm incorrect, i'm still a newbie

codingninja007 avatar Jul 24 '18 06:07 codingninja007

@codingninja007 I am not a pro, but the way you explained the problem worked for me to find a solution, that is, have the script load after the div and not before as naxogon also suggested.

AmaiBatate avatar Aug 13 '18 16:08 AmaiBatate

var pJS_targ = document.getElementById("particles-js"); if (pJS_targ) { pJS_val = document.getElementById("particles-js").value; console.log("pJS : " + pJS_val); require('./particles-sel.js'); //After a lot of research I got this solution for getting rid of the error getElementsByClassName }

//particles-sel.js contains code below

/* Handles Particles.js simulation*/ particlesJS("particles-js", { "particles": { "number": { "value": 250, "density": { "enable": false, "value_area": 5000 } }, "color": { "value": "#fff" }, "shape": { "type": "circle", "stroke": { "width": 0, "color": "#fff" }, "polygon": { "nb_sides": 5 }, "image": { "src": " ", "width": 100, "height": 100 } }, "opacity": { "value": 1, "random": false, "anim": { "enable": false, "speed": 8, "opacity_min": 0.1, "sync": false } }, "size": { "value": 9.5, "random": true, "anim": { "enable": false, "speed": 150, "size_min": 0.1, "sync": true } }, "line_linked": { "enable": false, "distance": 200, "color": "#fff", "opacity": .5, "width": 1 }, "move": { "enable": true, "speed": 10, "direction": "none", "random": false, "straight": false, "out_mode": "out", "bounce": false, "attract": { "enable": false, "rotateX": 1200, "rotateY": 1200 } } }, "interactivity": { "detect_on": "canvas", "events": { "onhover": { "enable": false, "mode": "grab" }, "onclick": { "enable": false, "mode": "push" }, "resize": false }, "modes": { "grab": { "distance": 400, "line_linked": { "opacity": 1 } }, "grab": { "distance": 400, "size": 40, "duration": 2, "opacity": 4, "speed": 3 }, "grab": { "distance": 200, "duration": 0.4 }, "push": { "particles_nb": 4 }, "remove": { "particles_nb": 2 } } }, "retina_detect": true }); var update; update = function () { if (window.pJSDom[0].pJS.particles && window.pJSDom[0].pJS.particles.array) {} requestAnimationFrame(update); }; requestAnimationFrame(update);

techno-trace avatar Sep 30 '18 10:09 techno-trace

I'm still struggling on this. I have a Node app using Express, I have the script at the end of my body before my footer and loading the id particles-js div before it but still getting this error. I believe i have my scripts in order of hierarchy as well. Any ideas please and thank you!

AwesomeZaidi avatar Nov 10 '18 03:11 AwesomeZaidi

I'm using Angular 6 and was facing this issue, the app worked fine in index.html but while loading from one of its component it was failing.

I used this following fix - In my component.ts file I declared particlesJS variable -

declare var particlesJS:any

Then in the function ngAfterViewInit I wrote all the function:

ngAfterViewInit(){ particlesJS('particles-js',

{ "particles": { "number": { "value": 80, "density": { "enable": true, "value_area": 800 } .....)}

Let me know if you're still facing the issue

LakshMatai avatar Nov 24 '18 09:11 LakshMatai

Try moving the script to the end of the Body, just before the footer

It did solve problem, by placing all the jquery libraries at the end of the body. Thank you! @naxogon

sallu161 avatar Jan 04 '19 04:01 sallu161

Nothing really worked for me... can't remember my exact solution but I just re did it from scratch all in one file and it worked then. Then, I isolated things one at a time testing at each phase to make sure it wouldn't break. Hope this helps anyone scratching their heads, sometimes if its a small enough thing you can just restart and avoid any improper module injections by doin it all in one file :)

AwesomeZaidi avatar May 26 '19 04:05 AwesomeZaidi

If you're using Angular and trying to separate things out into components, you should add this to index.html:

<div id="particles-js"></div>

codejockie avatar Jun 22 '19 23:06 codejockie

In my case I'm using react and very same problem occurs. First put your particles-js fucntion in index.html. Then in your UI component where you want to call function call it in there. For example I want to call it in class based component. So in the constructor I called it as below, constructor() { super(); window.test(); } (I wanted to use particles in my footer)

sashiksu avatar Feb 21 '20 13:02 sashiksu

You can use this https://github.com/matteobruni/tsparticles, it has also wrappers for jQuery, React, Vue, Preact and Angular ready to use.

matteobruni avatar Jun 14 '20 14:06 matteobruni

I had the same error and solved it likes this: My id name was "particle-js" but i had particles.js.load('particle-js)! The 's' was missing and therefore it did not work. I don't use angular or something else so it was a pretty stupid error I guess.

Kind regards from Italy, Daniel :)

DanielOberlechner avatar Jul 18 '20 23:07 DanielOberlechner

I used this following fix - In my component.ts file I declared particlesJS variable -

declare var particlesJS:any

@LakshMatai It works. Thank you so much! I was close I knew it! That I would probably need to do something like that in a component.

I'm using Angular 13.3.5. Here's a quick setup to make particle.js work in an Angular component:

import { Component, OnInit } from '@angular/core';

declare var particlesJS: (tagId: string, params: any) => void;
@Component({
  selector: 'app-hero-header',
  templateUrl: './hero-header.component.html',
  styleUrls: ['./hero-header.component.scss']
})
export class HeroHeaderComponent implements OnInit {
  ngAfterViewInit() {
    particlesJS("particles-js", {
      "particles": {
        "number": {
          "value": 67,
          "density": {
            "enable": true,
            "value_area": 800
          }
        }
      }
    }
    );
  }

  ngOnInit(): void {
  }
}

germanfica avatar May 18 '22 17:05 germanfica

You can use this https://github.com/matteobruni/tsparticles, it has also wrappers for jQuery, React, Vue, Preact and Angular ready to use.

@germanfica this is the easiest way, there's an angular component and everything is documented here: https://github.com/matteobruni/tsparticles/tree/main/components/angular#readme

With TypeScript support, and no custom code to be searched anywhere

markwrightdev avatar May 18 '22 17:05 markwrightdev