New to jQuery, I have a script that returns data from an XML file. What I need is the index number of the element returned. I know I can retreve a single element with this
name = $("sitelist:eq(1)",data).text();
but in my script I need to know each element position for the displayed data.
something like position = $(data,eq).value .Can anyone help please .
$(document).ready(function () {
$.ajax({
type: "GET",
url: "sites.xml",
dataType: "xml",
success: displayXml
});
function displayXml(data) {
$(data).find("course").each(function () {
var name = $(this).find("sitelist").text();
var line1 = $(this).find("address1").text();
});
}
}); // doc ready
I’m not sure which node you need the index for, but if it is the
coursethat you’re iterating over with.each()you can get the index of each iteration from.each()If there’s some other situation, you could try using the
.index()method to get the index of the node from among its siblings. It requires jQuery 1.4 or later.As in: