Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Get an element in the document and adding child elements

Get an element in the document and adding child elements

Problem

I've got a set of Elements called item-group-button in my DOM hierarchy. I use the following jQuery code to get the above specified element and append new content to the inner HTML of the element.

    function getMajorGroups(){
    var element = $(".item-group-button");
    $.get("http://localhost:9157/getAllMajorGroups", function(data){
        if(data.majorGroups.length != 0){
            $('.panel_list').empty();

        }
        for(var i = 0; i " + data.majorGroups[i] + "");
            $('.panel_list').append(element);
        }
    });
}

The element code is as following.

Beverage

But when I run the code, the elements don't get cleared and also the new content is appended with the current content.

What I'm trying to do is connect to a web service and according to the data received, populate the html elements. If any one have better suggestions please enlighten me!

thank you!

Problem courtesy of: Imesh Chandrasiri

Solution

I think you should use .clone() because from what I see in your code, you are appending the element again and again, producing some duplicate values.

function getMajorGroups(){
  var element = $(".item-group-button");
  $.get("http://localhost:9157/getAllMajorGroups", function(data){
    if(data.majorGroups.length != 0){
      $('.panel_list').empty();
    }
    for(var i = 0; i " + data.majorGroups[i] + "");
      $('.panel_list').append(clone);
    }
  });
}
Solution courtesy of: Arnelle Balane

Discussion

View additional discussion.



This post first appeared on CSS3 Recipes - The Solution To All Your Style Problems, please read the originial post: here

Share the post

Get an element in the document and adding child elements

×

Subscribe to Css3 Recipes - The Solution To All Your Style Problems

Get updates delivered right to your inbox!

Thank you for your subscription

×