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 7407593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:44:58+00:00 2026-05-29T05:44:58+00:00

Here’s my code (what I’m stuck on is after the jump) <script language=JavaScript type=text/javascript

  • 0

Here’s my code (what I’m stuck on is after the jump)

<script language="JavaScript" type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
var countThisMany = 4; //how many data-id's I want to count per "section" of images
var finalString = "";  //blank var to be used later, must be defined here.
</script>

<img src="foo.jpg" data-id="id1" data-item="1" />    // just a bunch
<img src="foo.jpg" data-id="id2" data-item="2" />    // of images with
<img src="foo.jpg" data-id="id3" data-item="3" />    // data-* usage to
<img src="foo.jpg" data-id="id4" data-item="4" />    // store two bits
<img src="foo.jpg" data-id="id5" data-item="5" />    // of information
<img src="foo.jpg" data-id="id6" data-item="6" />    // that I'll need
<img src="foo.jpg" data-id="id7" data-item="7" />    // to extract
<img src="foo.jpg" data-id="id8" data-item="8" />    // later on below.

<img src="bar.jpg" data-id="id9" data-item="1" />    // another set of images
<img src="bar.jpg" data-id="id10" data-item="2" />
<img src="bar.jpg" data-id="id11" data-item="3" />
<img src="bar.jpg" data-id="id12" data-item="4" />
<img src="bar.jpg" data-id="id13" data-item="5" />
<img src="bar.jpg" data-id="id14" data-item="6" />
<img src="bar.jpg" data-id="id15" data-item="7" />
<img src="bar.jpg" data-id="id16" data-item="8" />

<script type="text/javascript">
var item_array = $("img").map(function() {
    return $(this).attr("data-item");
}).get();

//alert(item_array);
//returns "1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8"


var id_array = $("img").map(function() {
    return $(this).attr("data-id");
}).get();
//alert(id_array);
//returns "id1,id2,id3,id4,id5,id6,id7,id8,id9,id10,id11,id12,id13,id14,id15,id16"


for (i=0;i<countThisMany;i++){
finalString=finalString+id_array[i]+';';
}

//alert(finalString);
//returns "id1;id2;id3;id4" -- could be more or less if "countThisMany" is changed to something other than 4 

</script>

This entire code is self-contained so feel free to save it as an HTML and uncomment the alerts to see the results.

What I need, however, is a step further. I would like to only collect the data-id of the img tags whose data-item are less than or equal to the variable countThisMany

So the end result of finalString would be “id1;id2;id3;id4;id9;id10;id11;id12”. And if countThisMany was changed to 2, for instance, it would be “id1;id2;id9;id10”

I feel as though I am so close but I can’t figure out how to only gather the first 4 of the first set of 8 images, and the first 4 of the second set of 8 images. Again, the total amount of images and how many I would need to capture vary, hence the “countThisMany’ var.

This is my first post to the amazing stackoverflow community, so I’m excited! Thanks to all contributors ahead of time!

  • 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-29T05:44:59+00:00Added an answer on May 29, 2026 at 5:44 am

    Use filter method

    var item_array = $("img").filter(function() {
        return parseInt($(this).data("item")) <= countThisMany;
    }).map(function() {
        return $(this).data('id');
    }).get();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is a simple timepicker to jQuery UI's datepicker <script type=text/javascript> /* <![CDATA[ */
Here is some code on the javascript side for form-based uploads: iframe.setAttribute('src', 'javascript:false;'); I'm
Here is my persistence.xml : <?xml version=1.0 encoding=UTF-8?> <persistence xmlns=http://java.sun.com/xml/ns/persistence xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd version=1.0>
here is my configuration: http://domain.com (obviously fictitious name...) hosted on a server running Apache
Here's my Manifest: <?xml version=1.0 encoding=utf-8?> <manifest xmlns:android=http://schemas.android.com/apk/res/android package=com.mappp.mobile android:versionCode=1 android:versionName=1.0 > <supports-screens android:largeScreens=true
Here is my response code in jQuery: var response = $.parseJSON(response); for (var i
Here is my code, which takes two version identifiers in the form 1, 5,
I am reading a book about Javascript and jQuery and using one of the
Here is an example: I write html code inside of textarea, then I swap
Here is an example: I have the generic type called Account. I wish to

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.