Shouvik Shouvik - 1 year ago 56
Javascript Question

Pass separate scope in each new Instance created in ES6

I have written some code as below. Here I am creating new instances for the class

and passing separate text each time to the constructor.

i am binding a click method to the element
so that it returns its the text associated with it on click of it.

Problem here is, the three components created by this method has separate texts displayed but on click of any element it displays the text as
'this is a todo component3'
which is the last text i passed into the constructor. I want it to be separate for each component. Please help.

class todos{
this.istodo = false;
this.text = text;
this.istodo = !this.istodo;
this.getDiv = document.getElementById('test');
this.getDiv.innerHTML = this.getDiv.innerHTML+'\n'+this.text;
this.getDiv.onclick = (event)=> {alert(this.text)}; //this is always coming as "this is a todo component3"

let todo = new todos('this is a todo component');
let todo1 = new todos('this is a todo component1');
let todo2 = new todos('this is a todo component2');
let todo3 = new todos('this is a todo component3');

Answer Source

The problem is that you have only one container for all your texts and every next todo you add overwrites onclick handler created by previous one. So this is the problem.

To solve it you just need to make sere every todo crates it's one container for the text. Here is one possible way to do it. Note, that document.getElementById('test') from the code.

    class todos {
      constructor(text) {
        this.istodo = false;
        this.text = text;
      _changestatus() {
        this.istodo = !this.istodo;
      setText() {
        this._node = document.createElement('div');
        this._node.appendChild(new Text(this.text));
        this._node.onclick = (event) => {
      getNode() {
        return this._node;

    let container = document.getElementById('test');

    ['this is a todo component', 'this is a todo component #2', 'this is a todo component #3'].forEach(text => {
      let todo = new todos(text);
<div id="test"></div>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download