Solved

Finding the "index" of a srcElement

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jQuery delay animation 3 31
Where is java script information saved? 2 55
How to create a textarea which saves text in HTML 8 37
JS does not refresh 6 21
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

825 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