I am trying to get an attribute value from an XML file using jQuery, but it returns undefined and the image doesn't load.
In my HTML file I have a <div>
with id answer
and one <button>
with id showInfo
.
xml code:
<?xml version="1.0"?>
<student studentNum="5678">
<image>images/mypic.jpg</image>
</student>
my jQuery code:
$(document).ready(function(){
$.ajax( {
url:'stu.xml',
data:{},
type:'GET',
dataType:'xml',
success: function(resp){
$('#showInfo').click(function () {
var myInfo = $(resp).find('student');
myInfo.each(function(index, obj) {
var myNum = ($(obj).find('student').attr('studentNum'));
var myImg = $(obj).find('image').text();
$('#answer').append("<p> " + "Student Number: " +myNum+
"<img src='" + myImg + "'/><br/>" );
});
});
});