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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:09:55+00:00 2026-05-31T13:09:55+00:00

I am working on javascript tool for a client site at the moment, basically

  • 0

I am working on javascript tool for a client site at the moment, basically the users selects a colour from a drop down list, and the client then wants that image for that colour to be displayed.

There are a view problems however, the system the client uses has no way of adding id’s or classes to the image, so I cant just do a simple,

if (select value = a class)
{
    showImage
}

What I have had to do so far, is add the images colour to the image’s alt text, so a image title might typically looks like this:

A sock, Green

So far I have the following code:

$('.information select#colours').change(function(){
    var colour = $(this).val();
});

On my page there can be any number of product images, and their markup will look like this,

<div class="carousel js" tabindex="0">
<ul class="alternative_images" style="position: absolute; top: 0px; width: 60px; height: 276px;">
    <li>
        <a href="/uploaded/thumbnails/db_file_img_4_254x347.jpg">
            <img alt="Ben Fogle Sock, Grey" src="/uploaded/thumbnails/db_file_img_9_58x79.jpg">
        </a>
    </li>
    <li>
        <a href="/uploaded/thumbnails/db_file_img_4_254x347.jpg">
            <img alt="Ben Fogle Sock, Green" src="/uploaded/thumbnails/db_file_img_7_58x79.jpg">
        </a>
    </li>
    <li>
        <a href="/uploaded/thumbnails/db_file_img_4_254x347.jpg">
            <img alt="Ben Fogle Sock, Red" src="/uploaded/thumbnails/db_file_img_5_58x79.jpg">
        </a>
    </li>
</ul>

My question is

1) How can I collate all the colours of the images that are found in the images alt after a comma?
2) How can I then search through those colours to pull back the correct image?

Is there a better way of doing this?

  • 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-31T13:09:56+00:00Added an answer on May 31, 2026 at 1:09 pm

    Several people have added working answers, but I’m thinking if you have enough images to filter by color you may be interested in performance, too, which means that selecting all of the image elements and parsing their alt text every time the filter option is changed isn’t the best solution.

    An alternative is to do this once, extracting the colors only on pageload and adding them as a CSS class on the images. Since modern browsers optimize class lookups with querySelectorAll (and jQuery uses this optimization whenever possible) you can then do a plain old class select and expect good performance:

    $(function() {
      var thisEl, thisAlt, lastCommaAt, thisColor;
    
      // select all of the images that have an `alt` attribute containing a comma
      $( '.alternative_images img[alt *= ","]' ).each( function() {
        thisEl  = $(this);
        thisAlt = thisEl.attr( 'alt' );
    
        // get the position of the last comma
        lastCommaAt = thisAlt.lastIndexOf( ',' );
        if( lastCommaAt > 0 ) {
          // get the part after the last comma and trim extra whitespace
          thisColor = $.trim( thisAlt.slice( lastCommaAt + 1 ) );
    
          // add the color as a CSS class
          thisEl.addClass( thisColor );
        }
      } );
    
    
      // later...
    
      $( '.information select#colours' ).change( function() {
        var selector     = '.alternative_images .' + $(this).val();
          , matchingImgs = $( selector )
        ;
    
        // do something with the matching images
        console.log( matchingImgs );
      });
    });
    

    (If performance is still an issue another option is to build a hash table (using a JavaScript object) e.g. var els = { "Red" : [ <img src...>, <img...>, ... ], "Blue" : [ <img...>, ...] } for very fast lookups e.g. els[ "Blue" ].)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been working on a tool to transcribe recordings of speech with Javascript. Basically
I'm currently working on a JavaScript tool that, during the course of its execution,
I'm working on building a development tool that is written in JavaScript. This will
I am working on a javascript app that users load via a configurable script
I am currently working on a site that includes javascript code that we get
We are working on a JavaScript tool that has older code in it, so
I'm working with a tool that only allows for Javascript as the scriptting language.
I need JavaScript working almost perfectly in my application that should be able to
I am working with javascript and here is what I am trying to do:
I'm using js2-mode for working with javascript in emacs and for the most part

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.