Ben 11

Hey guys, quick question . . .

I had asked previously how to have jquery perform a specific action, when it found matching rel attributes.

Looking for some further assistance with this. Basically, I'm taking a grid-layout of photos and modifying them. Some photos are related and some are not. The ones that are related will have matching Rel attributes.

Basically, I would like jquery to recognize a group of related images (with the same rel), hide all of the images except the first and apply a background image to the container of the first photo (not finalized yet but, it will likely be a div or li).

Any help with this would be greatly appreciated.

1 answer

o.k.w 2355
This was chosen as the best answer

Am not too sure how you want to hide the images, do their grid cell remain? Anyway here's one way of implementing it.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head><title>match rel</title>
<script type="text/javascript" src=""></script>
<script type="text/javascript">
$(function() {
    $(".photo img").show().css('background','transparent'); //restore visibility

function matchImg(relVal){
    var sel = ".photo img[rel='" + relVal + "']";
    if ($(sel).length > 0) { //check matching rel
        $(sel + ":gt(0)").hide(); //hide all except first
        $(sel + ":first").css('background','red'); //set background to first
<style type="text/css">
.photo img {
    padding: 5px;
    float: left;
<div class="photo">
<img rel="dt" src="" alt="1" />
<img rel="dt" src="" alt="2" />
<img rel="dt" src="" alt="3" />
<img rel="so" src="" alt="4" />
<img rel="so" src="" alt="5" />
<p style="clear: both;">
Enter 'dt' or 'so' <input type="text" value="dt" id="relval" />
<input type="button" value="match" />

To have the images filtered on page load based on a pre-determined text e.g. 'mytext', replace the code of $(function().. with this:

$(function() {
Answered almost 9 years ago by o.k.w
  • Thanks o.k.w. I'll have to play with that. How would I get it to do that automatically (with out the field). Ben almost 9 years ago
  • @Ben, I'm not sure exactly how you want to trigger the filtering of the images, hence I encapsulated the codes in the matchImg(relVal) method. I can only assist further if you can elaborate on it. E.g. upon clicking/hovering on some elements? or..??? o.k.w almost 9 years ago
  • @O.K.W., Basically, I don't want to have to specify the Rel attribute I would like matched. I would like Jquery to perform this function on any/all images that have the same Rel attribute, automatically. Each page will have multiple groups of Rel attributes. Ben almost 9 years ago
  • @Ben, I'm still not very clear about this. If matching is done for all image sets, and they are all hidden except the first one of each sets, why do you want the hidden images at all? o.k.w almost 9 years ago
  • @O.K.W. , These pages are auto generated by another piece of software. The images are placed there automatically. Im trying to use Jquery to group them Ben almost 9 years ago