$(this).getAttribute is not a function when using $.each XML Javascript



I am looping through an XML document and trying to get the name attribute for each one, however Chrome keeps telling me that $(this).getAttribute("name"); is not a function. Anyone have any idea what's going on?


I saw a similar post on this, but it was for passing $(this) in an onclick attribute in a button, not for iterating. Was hoping I could be enlightened on the matter.


XML:



<?xml version="1.0" encoding="UTF-8"?>
<user>
<hotel_group name="Pueblo Bonita" id="1">
<hotel>
<name>Pueblo Bonita 1</name>
<location>
<address>123 Deer St.</address>
<city>Montego Bay</city>
<state>St. Ann's Parish</state>
<country>Jamaica</country>
</location>
<hotel_code>556</hotel_code>
</hotel>
<hotel>
<name>Pueblo Bonita 2</name>
<location>
<address>123 Caribou Dr.</address>
<city>Negril</city>
<state>Spanish Town</state>
<country>Jamaica</country>
</location>
<hotel_code>555</hotel_code>
</hotel>
<hotel>
<name>Pueblo Bonita 3</name>
<location>
<address>30 Milsborough Dr</address>
<city>Kingston</city>
<state>Kingston</state>
<country>Jamaica</country>
</location>
<hotel_code>552</hotel_code>
</hotel>
</hotel_group>
</user>


JS:



$(xml).find("hotel_group").each(function (k, v) {
if (!hotelGroups) {
hotelGroups = [];
}

hotelGroups[k] = $(this).getAttribute("name");

});

No comments:

Post a Comment