Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Finding the "index" of a srcElement

Posted on 2000-04-22
5
Medium Priority
?
180 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 800 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

824 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