Gustavo Henrique Gustavo Henrique - 4 months ago 10
jQuery Question

Error getting the .last()

I have this structure:
https://jsfiddle.net/ror1mm0r/

I want to get the last message, im trying to use the .last() function:

$('#user-chat-' + data.de).find('#load-msgs').last().html()


But the jquery returns all the results inside the div, not the last result. Someone knows why im getting this error? (The selector is correct, im getting the result, but i don't get the last)
Thanks!

Answer
    $(document).ready(function(){

           $("#load-msgs").last().find('div span').html()

//OR
           $("#load-msgs div span").last().html()    
    })

Here is the working fiddle

The html from your script :

<div class="row" id="load-msgs">
    <div class="col-md-10 col-sm-12 col-xs-12 ">
        <span class="message-box message-other" data-toggle="tooltip" data-placement="right" title="18:28">
         lewpwa </span>
    </div>


    <div class="col-md-10 col-sm-12 col-xs-12 ">
        <span class="message-box message-other" data-toggle="tooltip" data-placement="right" title="18:28">
         oppa   </span>
    </div>


    <div class="col-md-10 col-sm-12 col-xs-12 ">
        <span class="message-box message-other" data-toggle="tooltip" data-placement="right" title="18:36">
         oppa   </span>
    </div>


    <div class="col-md-10 col-sm-12 col-xs-12 ">
        <span class="message-box message-other" data-toggle="tooltip" data-placement="right" title="18:36">
         oppa   </span>
    </div></div>
Comments