Ninjection
Ninjection

Reputation: 47

Using jQuery, how do I get the index of an element found in XML?

I have an XML file setup like so:

<entry name="bob"></entry>
<entry name="ryan"></entry>
<entry name="joe"></entry>
...
<entry name="etc"></entry>

Next, I have a line of code that picks out a name from the XML like so:

var $user= $('entry[images="' + userName + '"]', xml);

But how do I find out what the index of $user is in the overall XML? Example: if userName was 'joe', I should get the number '2' back. Any suggestions?

Upvotes: 3

Views: 118

Answers (3)

Blazemonger
Blazemonger

Reputation: 92893

jQuery's index() method is your friend. Look at this jQuery:

<script type="text/javascript">
window.onload = function() {
    $("div p").each(function() {
        $(this).append( $(this).attr("name")+$(this).index() );
    });
};
</script>

HTML:

<div>
<p name="tom"></p>
<p name="dick"></p>
<p name="harry"></p>
</div>

will produce:

tom0

dick1

harry2

Upvotes: 1

Manuel
Manuel

Reputation: 898

I believe that this is what you are looking for: http://api.jquery.com/index/

Upvotes: 1

You can use the .index() method: http://api.jquery.com/index/

Upvotes: 1

Related Questions