JQuery contains list elements containing the same text part

I have this HTML list:

  • Bob Smith
  • Bob Stone
  • Bob Miller
  • John Smith
  • John Doe
  • Jason McGee
  • I want to wrap the list elements containing the same Firstname like this

    
    
    

    I habe tried to iterate through the list to set up a array and create the new elements by writing this code (getID spilts the text to Bob and Smith for example):

    var menArr = [];
    $(".catLink").wrap("
  • "); $("#getStyles .lv3").each(function() { var txt = getID($(this).text()); $(this).wrapAll("
      ").before("
    • "+txt+"
    • "); menArr.push(txt); });
  • But all I get is this:

    
    

    …and so on.

    Can you help me fixing this?

    Try this,

    $("#getStyles .lv3").each(function() {
        txt=$(this).text().split(' ')[0];
        if(!$('#result').find('.'+txt).length)
        {
            $('#result').append('
    • '+txt+'
    '); } $('#result').find('.'+txt).append($(this)); });

    Working Fiddle

    Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

    本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
    酷辣虫 » 前端开发 » JQuery contains list elements containing the same text part

    喜欢 (0)or分享给?

    专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

    使用声明 | 英豪名录