Solved

Finding the "index" of a srcElement

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

747 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

11 Experts available now in Live!

Get 1:1 Help Now