I tried to display XML data to popup box. Now It displays all data of XML file data on the popup. But Actually, When I click open modal1, need to load only the first data set(array-0) from an XML file. Also when clicking open modal2, need to load only the second data set(array-1) from XML.How can I do it using js loop?
This is my code.
data.xml
<?xml version="1.0"?>
<paintings>
<painting>
<title>test</title>
<artist>Test Artist</artist>
<image>https://upload.wikimedia.org/wikipedia/commons/thumb/5/52/Liliumbulbiferumflowertop.jpg/220px-Liliumbulbiferumflowertop.jpg</image>
<price>850</price>
</painting>
<painting>
<title>test2</title>
<artist>Test Artist2</artist>
<image>https://upload.wikimedia.org/wikipedia/commons/thumb/5/52/Liliumbulbiferumflowertop.jpg/220px-Liliumbulbiferumflowertop.jpg</image>
<price>8503</price>
</painting>
</paintings>
index.html
<div class="container">
<a href="#openModal1" id="openModalBtn">open modal1</a>
<a href="#openModal2" id="openModalBtn">open modal2</a>
<div id="openModal1" class="modalDialog">
<div>
<input class="getAssignment2" type="button" value="Previous" id="prev">
<input class="getAssignment" type="button" value="Next" id="next">
<a href="#close" title="Close" class="close">X</a>
<div id="container"></div>
</div>
</div>
</div>
JavaScript code
$(document).ready(function(){
$.ajax({
type:"GET",
url:"data.xml",
dataType:"xml",
success:xmlParser
});
});
function xmlParser(xml){
$(xml).find("painting").each(function(){
$("#container").append(
'<div class="painting"><img src="' + $(this).find("image").text() + '" width="200" height="225" alt="' + $(this).find("title").text() + '" /> <br/> <div class="title">' + $(this).find("title").text() + '<br/>$' + $(this).find("price").text() + '</div></div>');
});
}