I try to append a new element with an increasing id. This is my attempt, but it always sets the id 1:
JSFiddle: http://jsfiddle.net/z0qwcuq9/
<div id="main"></div>
for (i = 0; i < 5; i++) { 
    var maxId = 0;
    $("#main .element *[id]").each(function() {
        if(maxId < $(this).attr("id")){ maxId = $(this).attr("id")}
    });
    maxId = maxId + 1;
    $('#main').append('<div class="element" id="'+maxId+'">');
}
By the way: Is this the correct way to append an element or do I have to set the class and id seperatly?
 
     
    