Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using insertAfter() in jQuery

I have this XML file,

<tour title="Bavaria, Austrian, and the Alps" baseCost="1799.99">
<availability start="2011/03/25" end="2011/9/30"/>
<description>

</description>

<stop day="1">
<title>Munich, Germany. </title>

<description></description>
<latitude>48.15</latitude>
<longitude>11.5833333</longitude>
<hotels>
<hotel name="Europa Hotel" stars="3" premium="0.0"/>
<hotel name="Excelsior Hotel" stars="4" premium="59.99"/>
<hotel name="Grand Hotel" stars="5" premium="199.99"/>
</hotels>

</stop>

<stop day="2">
<title>Garmisch, Germany.</title>

<description>Major destination of the Bavarian alps. Site of a major World Cup ski race.</description>
<latitude>47.5</latitude>
<longitude>11.0833333</longitude>
<hotels>
<hotel name="Europa Hotel" stars="3" premium="0.0"/>
</hotels>
</stop>

I have parsed everything I need but the problem is I need to show the output like this,

<title name>
 <stop name>
  <hotels names>
 <stop name>
  <hotels names>

But the output is coming like this,

<title name>
<stops names>
<hotels names>

All in one order only, What should I use, insertAfter() or after()?

Here's my jQuery code : http://pastebin.com/dmETKi9E

HTML Code:

 <body>
    <h1 id="header"></h1>
    <h2 id="stop"></h2>
    <p id="hotel"></p>
 </body>
like image 600
unknownsatan Avatar asked Feb 14 '26 13:02

unknownsatan


1 Answers

UPDATE Tested and Working The issue is with your HTML. All of the headers are going in with the header, all of the stops are going in the stop, and all the hotels are going in to the hotel, when instead you should be creating a new object for each new item. Look at the wrap() jquery command. What you are going to want to do is

function processXml(xml) {
  var header, stop, hotel;
  $(xml).find("tour").each(function() {
    header = $('<h1 class="header" />');
    header.html($(this).attr("title"));

    $(this).find("stop").each(function() {

      stop = $('<h2 class="stop" />');
      stop.html(($(this).find("title").text()));

      $(this).find("hotel").each(function() {
       hotel = $('<p class="hotel" />');
       hotel.html(($(this).attr("name")));
       stop.append(hotel);
      });

      header.append(stop);
    });
    $('body').append(header);
  });
}

You're welcome :)

like image 144
Branden S. Smith Avatar answered Feb 17 '26 02:02

Branden S. Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!