Hi I have the following script to look up an xml file and produce an ordered list
$(document).ready(function() {
$.ajax({
type: "GET",
url: "search_action.php" + string,
dataType: "xml",
success: disxml
});
})
} // function
function disxml(data) {
$(data).find('results').find('client').each(function(row) {
name = $(this).find('name').text();
var add1 = $(this).find('address1').text();
var add2 = $(this).find('address1').text();
var pcode = $(this).find('postcode').text();
var num1 = $(this).find('number1').text();
var num2 = $(this).find('number2').text();
var contact = $(this).find('contact').text();
var email = $(this).find('email').text();
display += "<a onclick='populate();'> <b>" + name + "</b> - " + add1 + "<br></a>";
})
divbox.html(display); // draw contents
}
function populate() {
}
this is the xml file that it is referencing
<results>
<client>
<name>Ascot Racecourse</name>
<address1>Berkshire</address1>
<address2/>
<postcode>SL5 7JX</postcode>
<number1/>
<number2/>
<contact>Alastair Warwick</contact>
<email>As per course</email>
</client>
<client>
<name>Aston Villa Football Club</name>
<address1>Villa Park</address1>
<address2>Birmingham</address2>
<postcode>B6 6HE</postcode>
<number1/>
<number2/>
<contact>Andrew Evans </contact>
<email>Info@avfc.co.uk</email>
</client>
<client>
<name>Asda 1 Year Celebration</name>
<address1>Park In Ipswich</address1>
<address2>Ipswich</address2>
<postcode>IP</postcode>
<number1/>
<number2/>
<contact/>
<email>Jonathan Stephenson</email>
</client>
</results>
it does all work fine , when I have my list I have a link on each line which when clicked calls the function 'populate'
When the script gets to the populate function I am really stuck as how I reference the particular results line that called the function , how can I find this ?
Thanks for any help and I hope it makes sense !!
This should do:
function disxml(data) {
$(data).find('results').find('client').each(function(row) {
var clientItem = this;
var name = $(clientItem).find('name').text();
var add1 = $(clientItem).find('address1').text();
var add2 = $(clientItem).find('address1').text();
var pcode = $(clientItem).find('postcode').text();
var num1 = $(clientItem).find('number1').text();
var num2 = $(clientItem).find('number2').text();
var contact = $(clientItem).find('contact').text();
var email = $(clientItem).find('email').text();
var link = $("<a href='#'><b>" + name + "</b> - " + add1 + "<br/></a>")
link.click(function(evt){
evt.preventDefault();
populate(clientItem);
});
divbox.append(link);
});
}
function populate(item) {
alert("Populating " + $(item).find("name").text());
}