JavaScript function is not called onChange of Checkbox javascript function not called onchange

I am repeating a OBJECT to render UI. Each row has a ItemName and a checkbox. Onclick of checkbox I should get the ID of that row. When I Run the page I am getting this error msg => Cannot read property 'addToCompare' of undefined

import React from 'react';

    export default class List extends React.Component {

        constructor(props) {
            super(props);

        }
        addToCompare(event){
              console.log('get id of row');
            }
        render() {
          var planList = [
                          {id: 1, "itemname": "Sunshine},
                          {id: 2, "itemname": "Global"},
                          {id: 3, "itemname": "Lifetime"}
                          ];
            return (
                    <div id="layout-content" className="layout-content-wrapper">
                      {
                        planList.map(function(item, i) {
                         return (
                           <div className="row" key={i}>
                              <h1>
                               <input type="checkbox" onChange={this.addToCompare.bind(this)} />{item.name}
                              </h1>
                           </div>
                           )
                         })
                       }
                     )
    }    
Answer:1

And If you're using Class declarations, you can use all ES6 features.

class Example extends React.Component {
  constructor(props) {
    super(props)
    this.addToCompare = this.addToCompare.bind(this)
  }

  addToCompare(event) {
    console.log('get id of row')
  }

  render() {
    const planList = [
      { id: 1, itemname: 'Sunshine' },
      { id: 2, itemname: 'Global' },
      { id: 3, itemname: 'Lifetime' },
    ]

    const rows = planList.map((item, i) =>
      <div className="row" key={i}>
        <h1>
          <input type="checkbox" onChange={this.addToCompare} />
          {item.itemname}
        </h1>
      </div>
    )

    return (
      <div id="layout-content" className="layout-content-wrapper">
        {rows}
      </div>
   )
  }
}

ReactDOM.render(
  <Example />,
  document.getElementById('app')
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app" />
Answer:2

You are passing wrong context to the onChange method

class Example extends React.Component {
  constructor(props) {
    super(props);
  }
  addToCompare(event) {
    console.log('get id of row');
  }

  render() {
    var planList = [{
      id: 1,
      "itemname": "Sunshine"
    }, {
      id: 2,
      "itemname": "Global"
    }, {
      id: 3,
      "itemname": "Lifetime"
    }, ];

    var _this = this

    var rows = planList.map(function(item, i) {
      return (
         <div className="row" key={i}>
            <h1>
             <input type="checkbox" onChange={_this.addToCompare.bind(_this)} />{item.itemname}
            </h1>
         </div>
         )
      }
    )

    return (
      <div id="layout-content" className="layout-content-wrapper">
        {rows}
      </div>
   )
 }
}

ReactDOM.render(
  <Example />,
  document.getElementById('app')
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app" />
Answer:3

Step by step i did these Create a new Facebook App (App ID: 544557495732050) Create a new Facebook Page (https://www.facebook.com/Pasha-electronic-502844156580479/) In App which created by step 1 ...

Step by step i did these Create a new Facebook App (App ID: 544557495732050) Create a new Facebook Page (https://www.facebook.com/Pasha-electronic-502844156580479/) In App which created by step 1 ...

  1. facebook comments mirroring
  2. facebook comment mirroring deprecated

For example, I want to write something like: const initial = [1, 2, 3, 4]; const indexIWantToChange = 2; const obj = { prop: () => { const newArr = [...initial]; newArr[...

For example, I want to write something like: const initial = [1, 2, 3, 4]; const indexIWantToChange = 2; const obj = { prop: () => { const newArr = [...initial]; newArr[...

  1. change element array javascript
  2. change element array python
  3. change element array numpy
  4. change array element position javascript
  5. change array element value javascript
  6. change array element java
  7. change array element
  8. change array element position php
  9. change array element php
  10. change array element value php
  11. change array element matlab
  12. change array element name unity
  13. change array element c
  14. change array element typescript
  15. change array element bash
  16. change array element swift
  17. change array element position js
  18. change array element value
  19. change array element key php
  20. change array element position lodash

I try to pass a variable from Javascript (when choosing some option in a form) to a PHP file which calls a SQL query. The SQL query (a string) should be handed over to a Javascript function and the ...

I try to pass a variable from Javascript (when choosing some option in a form) to a PHP file which calls a SQL query. The SQL query (a string) should be handed over to a Javascript function and the ...

  1. pass variable javascript to php
  2. pass variable javascript
  3. pass variable javascript to html
  4. pass variable javascript function
  5. pass variable javascript to java
  6. pass variable javascript to c#
  7. pass variable javascript function onclick event
  8. pass variable javascript to php ajax
  9. pass javascript variable to php function
  10. pass javascript variable to c# razor
  11. pass javascript variable to html input value
  12. pass javascript variable to php without ajax
  13. pass javascript variable to php function in same page
  14. pass javascript variable to razor
  15. pass javascript variable to django template
  16. pass javascript variable to python script
  17. pass javascript variable to php ajax jquery
  18. pass javascript variable to laravel route
  19. pass javascript variable to django
  20. pass javascript variable to php same page ajax

I want to use $timeout inside my custom AngularJS directive, but it's not working. My last implementation looks as following: var App = angular.module('App', []); App.controller('Controller', ...

I want to use $timeout inside my custom AngularJS directive, but it's not working. My last implementation looks as following: var App = angular.module('App', []); App.controller('Controller', ...