/    Sign up×
Community /Pin to ProfileBookmark

index order of an object in an array

I have

<a href=”#” onclick=”showOrder(this)”>zero</a>
<a href=”#” onclick=”showOrder(this)”>one</a>
<a href=”#” onclick=”showOrder(this)”>two</a>

In code. these objects are elements in an array:

hrefArray = document.getElementsByTagName(‘a’);
thus i have 3 elements (objects)
hrefArray[0]
hrefArray[1]
hrefArray[2]

how can return on click, in function showOrder(), the order of these elements (0,1 or 2)?

function showOrder(which){
var order = which.[color=red][i]what_attribute_here?[/i][/color];
alert(order);
}

?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@KorauthorMay 20.2004 — well, I solved the problem by compare the two objects

for(i=0;i<hrefArray.length;i++){

if(which==hrefArray[i]){alert(i)}

}



Still I wonder if there is a property or method to return directly the order



if there is

document.getElementById('theid').id

could not be something like

document.getElementsByTagName('tagname')[0].[i]order[/i]?
Copy linkTweet thisAlerts:
@FangMay 20.2004 — You can't; the only attribute that is defined is [I]href[/I](and the onclick itself), so "which" returns the url.

Easy enough using the DOM, as you did, to help you.
Copy linkTweet thisAlerts:
@KorauthorMay 20.2004 — thanx
×

Success!

Help @Kor spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...