How to find clicked element in array of react elements?

How to find clicked element in array of react elements?


I have on one side array of elements that will be rendered, and on other side click handler. So issue is in click handler function when i check indexOf(this) in array of elements, i get -1;


I don’t think you can call this.state.html.indexOf(this), because this is just your whole React component! So you’re asking if your React component is inside of the array, which is why you always get -1.

You might be able to create a new component for your black box (e.g. called BlackBox), and then construct your array using those instead.

I’m just spitballing here, but your clickHandler might then pick up that “this” refers to the individual box (the smaller component) rather than the parent component.

I don’t know, that’s what I would try next. Perhaps somebody else has a better idea.


I would add an ‘id’ attribute to your div that is equal to i. Then when you click, the would be your array index.


I think the is actually null for some reason. The click event isn’t working properly.


i want to escape numeration

        <div className="node" onClick={this.clickHandler.bind(this, i)}> </div>
     return ({ html: arr });
  clickHandler: function(i){



In React that are ways to handle all of the above without the use of jQuery, which I think is generally preferred.

In this case, the array of data could be generated in a similar way but rendered to the page using the map function, which would basically iterate through the array and allow you to specify what to render for each element. You could bind the index of the item to each rendered item in that way, and make it accessible using an onClick you attach as well.

For example if you have an array like this in your state:

var array = [1,2,3,4,5];

You could return in your render() function something like:, index) {

return <div className = "node" onClick = {this.handleClick.bind(this, index)}>{item}</div>


This should return 5 divs with content 1, 2, 3, 4, 5, and click handlers bound to each div. You would then need a handleClick() function in your component which would be triggered on the click of any of those divs and could just return its index, which would be passed into the function from the clicked div.

function handleClick(index) { console.log(index); }

I think that should point you in a helpful direction.

Btw I’ve been using ES6 with React so my syntax *might be a little off above for ES5 but I think the ideas are correct at any rate! Good luck!


i dont want to use numeration, i want to compare objects


Why? There are a couple of suggestions that would clearly work for what you want to accomplish.


If i have

and then user delete node-3


and then when user add another node i need to keep reference to number that is suffix to node so it can be easy with session or local storage, but i don’t want to lean on them, because if we have


and i set suffix to be length of array found in local storage

it will be:


I have no idea what you want accomplish but here you have it:

getInitialState: function(){
    this.realHTML = [];
    var arr = [];
    for ( let i = 0; i < 3; i ++ )
         <div className="node" ref={(ref) => this.realHTML[i] = ref}>  "feaf " </div>
    return ({ html: arr });
  componentDidMount: function(){
    $(".node").on("click", function(event ){
      console.log("event: " + $(;
      console.log("html content: " + this.state.html);
      console.log("index of event in html content: " + this.realHTML.indexOf(;

indexOf is not -1 any more.


Something like this, but this doesn’t work:

console.log("indexOf this in arr is: " + arr.toArray().indexOf(this));


hacked this indexOf by this:
code: 99 - 119 line

but below line 119. how i change properties like css and className, when i have just props of object from array. I heard props is immutable. how to jquery this?

**EDIT: just first click close to this pop up with tomato picutre, cause this lean on my elements below