Get last id of dynamic div

Couldn't find an answer, but I'm trying to get the last id of the last div that was created on the page. The code I'm using doesn't seem to work using .last (). My syntax is probably wrong, but I can't get it to show the id.

  jQuery(document).ready(function()
    {jQuery("a.more_posts").live('click', function(event){
            jQuery("div[id^='statuscontainer_']").last();
            var id = parseInt(this.id.replace("statuscontainer_", ""));
            alert(id);
       }); 
    });

      

I decided to do it like this and it worked.

jQuery(document).ready(function(){  
jQuery("a.more_posts").live('click', function(event){
        jQuery("div[id^='statuscontainer_']:last").each(function(){
        var id = parseInt(this.id.replace("statuscontainer_", ""));
        alert(id);
    }); 
}); 
}); 

      

+2


a source to share


1 answer


This .id will give you the id of the link that was clicked. I think you want the id of the last container.



jQuery(document).ready(function()  {
       jQuery("a.more_posts").live('click', function(event){ 
            var lastContainer = jQuery("div[id^='statuscontainer_']").last(); 
            var id = parseInt(lastContainer.attr('id').replace("statuscontainer_", "")); 
            alert(id); 
       });  
    }); 

      

+1


a source