Solved

Finding the "index" of a srcElement

Posted on 2000-04-22
5
169 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now