Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8919777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:06:35+00:00 2026-06-15T06:06:35+00:00

I have 3 divs with class: wpEdit and onClick: alertName() <div class=wpEdit onClick=alertName()>Bruce Lee</div>

  • 0

I have 3 divs with class: wpEdit and onClick: alertName()

<div class="wpEdit" onClick="alertName()">Bruce Lee</div>
<div class="wpEdit" onClick="alertName()">Jackie Chan</div>
<div class="wpEdit" onClick="alertName()">Jet li</div>

When clicked i want to know the Index of class wpEdit of the clicked Div:

function alertName(){
    //Something like this
    var classIndex = this.className.index; // This obviously dosnt work
    alert(classIndex); 
}

when clicked on Bruce Lee it should alert : 0
when clicked on Jackie Chan it should alert : 1
when clicked on Jet Li it should alert : 2

I need to know which instance of class=”wpEdit” is clicked on

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:06:35+00:00Added an answer on June 15, 2026 at 6:06 am

    Try this

    function clickedClassHandler(name,callback) {
    
        // apply click handler to all elements with matching className
        var allElements = document.body.getElementsByTagName("*");
    
        for(var x = 0, len = allElements.length; x < len; x++) {
            if(allElements[x].className == name) {
                allElements[x].onclick = handleClick;
            }
        }
    
        function handleClick() {
            var elmParent = this.parentNode;
            var parentChilds = elmParent.childNodes;
            var index = 0;
    
            for(var x = 0; x < parentChilds.length; x++) {
                if(parentChilds[x] == this) {
                    break;
                }
    
                if(parentChilds[x].className == name) {
                    index++;
                }
            }
    
            callback.call(this,index);
        }
    }
    

    Usage:

    clickedClassHandler("wpEdit",function(index){
        // do something with the index
        alert(index);
    
        // 'this' refers to the element 
        // so you could do something with the element itself
        this.style.backgroundColor = 'orange';
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple divs (haveing same class names). I want to move the div
I have 8 divs. Each div has the class 'pre-loader'. In addition to thas
I want to display some div's in a wrap. The div have same class
I have few divs on a page with same class. I want to use
I have 2 divs with 2 different class: div class name is: good div
I have two divs as follows and I want to change the class name
I have several divs with one class on a page. <div class=main/> <div class=main/>
I have four divs each with the same ID but different class. <div id=inner
We have some divs off class 'divBG', we want each of them to fade
I have 20 divs with the same class (bio): <div class=bio data-id=<? echo $id;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.