JavaScript Javascript get index of child on click javascript index child,javascript child index in parent,javascript index

I've added a click event to a parent #carousel-thumbs.

carouselThumbsContainer.onclick = function(ev) {
    var target = ev.target;    // which child was actually clicked
}

<ul id="carousel-thumbs" class="l-grid">
    <li class="active"><a class="active" target='_blank' href="#"><img src="img/carousel-1-th.jpg" /></a></li>
    <li><a target='_blank' href="#"><img src="img/carousel-2-th.jpg" /></a></li>
    <li><a target='_blank' href="#"><img src="img/carousel-3-th.jpg" /></a></li>
    <li><a target='_blank' href="#"><img src="img/carousel-4-th.jpg" /></a></li>
</ul>

I would like to return the index of the clicked element relative to its parent. So if the user clicked on the second

  • I would get 1.

    /////////////////////////////////////////// Current solution which works but I was hoping to simplify:

        //Add a click event to each thumn in the thumbs container
    for (var j = 0, len = carouselThumbsContainer.children.length; j < len; j++){
        (function(index){
            carouselThumbsContainer.children[j].onclick = function(){
                  console.log(index);
            }    
        })(j);
    }
    

    Not knowing a ton about Javascript I thought there must be an easier way but perhaps not.

    Answer:1

  • Let's say we have a ball that can be moved left and right around the screen. When you click space, the ball should jump. I got the ball to move left and right in a canvas. However, when the ball is ...

    Let's say we have a ball that can be moved left and right around the screen. When you click space, the ball should jump. I got the ball to move left and right in a canvas. However, when the ball is ...

    I am unable to append values into the _summary class or any other. I believe its a really simple syntax error I am making. I have been trying different syntax's for the past day, with no luck. I am ...

    I am unable to append values into the _summary class or any other. I believe its a really simple syntax error I am making. I have been trying different syntax's for the past day, with no luck. I am ...

    1. append event jquery
    2. append event
    3. append event listener
    4. append event javascript
    5. append event listener jquery
    6. append event handler jquery
    7. append event onclick
    8. append event handler c#

    Here is my d3.js code , from here on right click i am calling the function contextmenu // Display the tiles for each bucket. var tile = svg.selectAll(".tile") .data(buckets) .enter().append("...

    Here is my d3.js code , from here on right click i am calling the function contextmenu // Display the tiles for each bucket. var tile = svg.selectAll(".tile") .data(buckets) .enter().append("...

    1. right click opens link in new tab
    2. right click opens windows installer
    3. right click opens new window
    4. right click opens on left
    5. right click opens properties
    6. right click opens link
    7. right click opens window
    8. right click menu opens on left side
    9. right click automatically opens
    10. autocad right click opens help
    11. firefox right click opens link
    12. shift right click opens powershell
    13. double right click opens properties
    14. right click menu opens slow
    15. chrome right click opens new tab
    16. right click mouse opens
    17. double click opens right click menu

    I am developing inside of a Google Chrome Extension. In this environment, I have the ability to inject JavaScript into third-party websites after requesting the appropriate permissions. As such, I am ...

    I am developing inside of a Google Chrome Extension. In this environment, I have the ability to inject JavaScript into third-party websites after requesting the appropriate permissions. As such, I am ...