question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to get the index of clicked element?

See original GitHub issue

I have some list item, and I want know which one is clicked by user, so I set onClik on every <li> item, and I user event.target to get the node clicked, but I get this issue, my <li> items has children nodes, when user click the children inside the <li> element, event.target is not the element I want, how do I fix it?

my code:

var React = require('react');
var ReactDOM = require('react-dom');

var CC = React.createClass({

    handleClick: function(e) {
        console.log(e.target.getAttribute('data-key'));
    },

    render: function() {
        var s = {
            border: '1px solid #ddd',
            display: 'block',
            padding: '10px'
        };
        var items = this.props.arr.map(function(a, i) {
            return (
                <li key={i} onClick={this.handleClick} style={s} data-key={i}>
                    <img src="images/joy.png"/>
                </li>
                );
        }, this);

        return (
            <ul >
                {items}
            </ul>
            );
    }
});


var App = React.createClass({
    getInitialState: function () {
        return {
            arr: [1,2,3]
        };
    },

    render: function() {
        return (
            <div>
                <CC arr={this.state.arr}/>
            </div>
        );
    }
});

ReactDOM.render(<App/>, document.getElementById('app'));

when user click the <img> inside <li>, e.target.getAttribute('data-key') return null

I do can fix it by add the data-key attribute to all of <li> element’s children, like:

var items = this.props.arr.map(function(a, i) {
    return (
        <li key={i} onClick={this.handleClick} style={s} data-key={i}>
            <img src="images/joy.png" data-key={i} />
        </li>
        );
}, this);

but I don’t think it is a elegant way.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:13
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

72reactions
yangshuncommented, Dec 17, 2015

I have written a jsfiddle to show you how it can be done without using data attributes at all. Basically you bind the index to the handleClick function and it will be passed into it as a parameter when it’s called.

https://jsfiddle.net/qmkqd7u6/3/

Also, this is a usage question, rather than a bug in the React core. Usage questions are better answered on sites like StackOverflow, try to use github issues for tracking bugs in the React core.

15reactions
jimfbcommented, Dec 17, 2015

@littlee Every minute we spend answering usage questions on github is one fewer minutes we can spend fixing React to make it better for everyone. That’s why we push people toward StackOverflow, where the answers are generally just as good (sometimes better) and don’t consume disproportionate amounts of our time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Get index of clicked element using pure javascript
//first store array in a variable let container = [...document.getElementById('container')]; //loop through array with forEach function ...
Read more >
Find the index of the element that was clicked with jQuery
There may be times when you need to know the index of the element that is clicked from within a group of elements...
Read more >
How to get index of an element onclick in JavaScript? - Reddit
In the generated form, use a hidden field to hold the index so you dont have to serial-search each element, or use an...
Read more >
How to get particular index by click event in js - Sololearn
I am confused to get index by click event a b If we get document.getElementsByClassName("list"); It will return an array so I.
Read more >
Get index of the clicked element - Google Groups
Right now I have the following javascript code to get the index: ... ... But the problem is the if the target element...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found