Solved

Finding the "index" of a srcElement

Posted on 2000-04-22
5
172 Views
Last Modified: 2006-11-17
I have several "items" on a web page with the same ID. When I call a function from any of the items I can get the item name by using
srcElement.id . Does anybody know how I can get the index of the srcElement, i.e. is it the 1st, 2nd or 3rd item called "MyDiv".

I know I can reference a particular element in an arry by using:

document.all.item("MyDiv",0).innerText
 
Makes sense? I hope so. Thanks for any help you can give.
 
Peter
0
Comment
Question by:eccles
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 7

Expert Comment

by:nettrom
ID: 2739635
Your code is against the recommendations found in the HTML 4.0 specification.  An ID should be a _unique_ identification, so you're not allowed to have multiple elements with the same ID.

If you want to group items together use "class".

As how to check for your element, you can do a comparison of the element directly:

var myElements = document.all.tags("div");
for(i = 0; i < myElements.length;i++) {
  if(myElements[i] == event.srcElement) {
    alert("ping!");
 }
}
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 2739774
var ElementName = srcElement.id, ItemCount, CurrentItem

for(i=0;i<document.all.length;i++)
{
    if(document.all[i].id==ElementName)
    {
        ItemCount++;
        if(document.all[i].focus)
           CurrentItem=ItemCount;
    }
 
   CurrentItem now contains the item which was selected
}
0
 
LVL 10

Expert Comment

by:kollegov
ID: 2739803
If you need to find index among
divs with the same ID, than this is better way

<script>
function doFind(e){
 for(i=0;i<document.all['some'].length;i++){
      if(document.all['some'][i]==event.srcElement){
        alert(i)
      }
 }
}

document.onmouseup=doFind;
</script>

<div id="some" >  abcd </div>
<div id="some" > abcd </div>
<div id="some" >  abcd </div>

0
 
LVL 10

Accepted Solution

by:
kollegov earned 200 total points
ID: 2739812
BTW you can use :-)
document.all["some"][i].innerText

<script>
function doFind(e){
 for(i=0;i<document.all['some'].length;i++){
      if(document.all['some'][i]==event.srcElement){
        alert(document.all["some"][i].innerText)
      }
 }
}

document.onmouseup=doFind;
</script>

<div id="some" >  a </div>
<div id="some" >  b </div>
<div id="some" >  c </div>

-------
Script tested with MIE5/Win95

Virtual_Max

0
 

Author Comment

by:eccles
ID: 2740352
Thanks for your help. It also works with IE5.5 and Win2000 as well.

0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question