populate jQuery UI accordion with xml, binding problem

I am populating jQuery accordion from simple xml file, I can get my data and append it as html to simulate accordion markup. Then I call for accordion, it won't work!

I guess the problem is binding newly created data to DOM, I have tried .live() and .delegate with no success.

How should I proceed?

Here is simplified example of my code (sorry that some of the names and comments are in finnish :-)) here is the link http://www.equstom.fi/hanuri.html

$('#valitsija').click(function() {
 $.get('http://www.equstom.fi/kurssit.xml', function(data) {
    $('#accordion').empty();
  $(data).find('Kurssi').each(function() {
  var $kurssi = $(this);
  var html = '<div>';
  html += '<h3><a href="#">' + $kurssi.find('KurssinNimi').text()  + '</a></h3>';
  html += '<div>' + $kurssi.find('KurssiKuvaus').text() + '</div>';
  html += '</div>';
  $('#accordion').append($(html));
  }); 
 });
}); 

/* ********** haetaankurssit loppu ****** ******/ // Accordion $("#accordion").accordion({ header: "h3" });

http://www.equstom.fi/hanuri.html

Answers


Note the two lines below I added (with comments). You need to destroy and then recreate the accordion.

    $('#valitsija').click(function() {
    $.get('http://www.equstom.fi/kurssit.xml', function(data) {

        //you need to destroy the accordion first
        $('#accordion').accordion("destroy");
        $('#accordion').empty();

        $(data).find('Kurssi').each(function() {

            var $kurssi = $(this);
            var html = '<div>';
            html += '<h3><a href="#">' + $kurssi.find('KurssinNimi').text()  + '</a></h3>';
            html += '<div>' + $kurssi.find('KurssiKuvaus').text() + '</div>';
            html += '</div>';
            $('#accordion').append($(html));

        }); 

        //you need to re-make the accordion
        $("#accordion").accordion({ header: "h3" });
    });
});

I would suggest storing the #accordion into a variable so you don't have to keep searching for it. This is called caching. (I know its not your question, but figured I'd offer this suggestion anyways). Something like this:

$('#valitsija').click(function() {
    $.get('http://www.equstom.fi/kurssit.xml', function(data) {

        var acc = $('#accordion');
        //you need to destroy the accordion first
        acc.accordion("destroy");
        acc.empty();

        $(data).find('Kurssi').each(function() {

            var $kurssi = $(this);
            var html = '<div>';
            html += '<h3><a href="#">' + $kurssi.find('KurssinNimi').text()  + '</a></h3>';
            html += '<div>' + $kurssi.find('KurssiKuvaus').text() + '</div>';
            html += '</div>';
            acc.append($(html));

        }); 

        //you need to re-make the accordion
        acc.accordion({ header: "h3" });
    });
}); 

Proof that it works


Need Your Help

Using geolocalisation with windows phone, await error

asynchronous geolocation windows-phone-8 async-await

I'm trying to get the current location of an user, but the code show me an error...

Delphi - Declaring in class or not?

delphi scope

Just recently, probably because I've been maintaining some old code, I've started to look at how / why I do things. As you do.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.