Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4267308
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:52:54+00:00 2026-05-21T06:52:54+00:00

I need to select and group a set of image links based on their

  • 0

I need to select and group a set of image links based on their <DIV> or <UL> container.

For example, in this HTML: select the first two links, attach my function on them, select the next two links, run the function … and so on

DIV
  LINK
  LINK

  DIV
    LINK
    LINK

    UL
      LINK
      LINK
    /UL

  /DIV

  UL
    LINK
    LINK
  /UL

/DIV

LINK
LINK

...

(each div/ul can have any number of links)

Right now I have this:

$('div').each(function(index){
  var elements = $(this).find('a').filter(function(){
    return !!this.href.match('(\.jpg|\.jpeg|\.png|\.gif)$');
  });

  console.log('------' + index + '------');
  console.log(elements);
});

which filters down the links to only to ones that are images.
but it doesn’t really group them like I want and my function runs more than once on the same element…

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T06:52:55+00:00Added an answer on May 21, 2026 at 6:52 am

    Because you probably have elements between the DIV’s/UL’s and the A, such as LI, you can start by selecting the A’s and “group” them:

    var groups =  [];
    $('a')
      .filter(function(){ return !!this.href.match('(\.jpg|\.jpeg|\.png|\.gif)$'); })
      .map(function(idx,el) { return [$(this).closest("div, ul"), $(this)]; })
      .each(function(idx,el) { 
            var c= el[0];
            var imglink = el[1];
            if(!c.data("groupId")) {
               c.data("groupId", groups.length);
               groups.push({ "container": c, "links": [] });
            }
            groups[c.data("groupId")].links.push(imglink);
      });
    
    // optional: remove the "groupId" data() from the container DOM elements:
    $.each(groups, function(idx,el) { el.container.data("groupId", null); });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a set of tasks that I need to select distinct on (group
I need to select statements where is fixed post id, group by user id
I need to select elements of a certain class except children of $(this). The
I need to select elements depending on first letter of text value of a
I need to select all entries between two given dates (Nov 10 and Jan
I need to get the nid alone in this query. SELECT count(nid) as total,nid
Similar to this question , I need to group a large number of records
This is query I have at the moment: SELECT `groups`.`id`, `groups`.`name`, `groups`.`description`, `groups`.`members`, `groups`.`image`,
I need some help on a MySQL SELECT statement. Unfortunately, this statement doesn't work
I need to fetch a node set containing elements that refer to exactly two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.