Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get index of this array element

Status
Not open for further replies.

joelmac

Programmer
Jun 4, 2002
92
CA
Hi there,

I have an array of spans with the onClick set something like this:

Code:
<span id="aSpace" onClick="span_onClick(this);"></span>

In my span_onClick() function I want to know which span was clicked by getting the span's index in the array. Anyone know the function That I can call to get the elements array index?

Also, does anyone know of a good javascript reference where I can find these answers from myself?

________________________
JoelMac
 
Why do you need the index? You might be able to use your functionality without the index, using just "this". Are all your spans named the same?

*cLFlaVA
----------------------------
Breaking the habit...
 
hmmmm, now that I've thought about it more you may be right, but I may need to know where I am in the array so that I can modify other elements that are near by, like the element before or after this one.

________________________
JoelMac
 
Well, let me give you a hypothetical.

say span_onclick(this) bolds this particular span, and removes bold from all other spans in the array.

This is how you could go about doing it:

<span id="span1">hello</span><br>
<span id="span2">goodbye</span><br>
<span id="span3">shut up</span><br>


function span_onClick(span_we_want) {
var s;
var span_id;
for (var i=1;i<=3;i++) {
span_id = 'span' + i.toString();
if (span_id == span_we_want.id) {
document.getElementById(span_id).style.fontWeight = 'bold';
} else {
document.getElementById(span_id).style.fontWeight = 'none';
}
}
}

*cLFlaVA
----------------------------
Breaking the habit...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top