Link to home
Start Free TrialLog in
Avatar of nanharbison
nanharbisonFlag for United States of America

asked on

Add links and a class to images on a page

I have several images on a page, which I want to add links  and a class to dynamically because I am using Drupal and can't add this directly to the images so I am creating a script to do it. I have tried in javascript and jquery, either would be fine or AJAX, if that works. But I can't get anything to work. Javascript is not one of my strengths.
The images don't have IDs, and each image should link to the big version of the image which is just the src on the page. I would like to add
rel="lightbox[custom]"
to each a href as well.
The page is here:
http://harbisondevelopment.info/graveyard-sky
ASKER CERTIFIED SOLUTION
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nanharbison

ASKER

tommyBoy, wow, this is great. I thought that by adding the rel="lightbox[custom]",  and class = b, the images were going to do what the images on this page are doing when you click on them:
http://harbisondevelopment.info/custom-japanese-style-swords

 If this is a new question, just let me know, and I will award the points and ask a new one.
Thanks for much for letting me stop tearing my hair out. I have to say jquery is fabulous, but I have never gotten it to work.
That's a fair assumption provided everything required by lightbox is in place (all js libraries and css).

I have no experience with Drupal or Lightbox.

You can delete this line in the script I provided because it changes the original image source. On the working page, the img src and the anchor href are the same value:

largeImgSrc = largeImgSrc.substring(0, largeImgSrc.lastIndexOf('.')) + '_large.jpg';

The order of execution may also be a factor. I have the jquery code executing on document ready. Perhaps certain code in the lightbox.js is acting on the images BEFORE the jquery script runs so lightbox fails to see the new rel attribute and class on the images. Just a thought. As a test, move the jquery script without the $(document).ready wrapper to the bottom of the <body> and put the lightbox.js include after that at the very bottom of the <body>. If that works then you'll know it's the order of execution making the difference. If not, there may be another js file required that I am unaware of, someone would have to dig deeper. I may be able to do it later today but a new question may get a quicker response.
I am in meetings most of today, so I won't have time to look at this until the evening. I am in no rush to get this done.   Thanks for your help and suggestions
No problem. Let me know if both of those suggestions fail.
Thanks for this. I wish I could give you more than 500 points after being frustrated for days not getting anything to work. I don't have the lightbox part figured out. I think that's a new question, so please look for it if you are interested. I am going to ask that.
It didn't work to put the lightbox js after it loads.
Thanks for the points.

I played with this a little bit during my lunch break and came up with a working local copy of your site in which the lightbox feature functions properly. To accomplish this I had to do a few things.

1.) After reading the lightbox website, I realized that the rel="lightbox" needs to go in the anchor tag, not the image tag so I changed the script in the <head> section to this:
<script type="text/javascript">
$(document).ready(function(){
      $('img[typeof="foaf:Image"]').each(function(){
            var largeImgSrc = $(this).attr('src');
            $(this).attr('class', 'b');
            $(this).wrap('<a href="' + largeImgSrc + '" rel="lightbox[custom]"></a>');
      });
});
</script>

2.) I downloaded a fresh copy of lightbox2 from their website, saved the files lightbox.js, jquery-1.7.2.min.js, and lightbox.css and made references to the local copies at the top of my test page.  It would not work with the references you already had included (maybe because it's Drupal-ized?).

3.) I commented out your reference to prototype.js. It must be a corrupt file because lightbox totally fails with that included.

I'm attaching my test page.
test-40.html
I had already moved the lightbox reference to the anchor tag. I downloaded the new version of lightbox, and had to make some other adjustments and after several more hours of fussing with it,
I also removed the two other js files, scriptaculous and mouseover. That's when it finally worked.
IT WORKS!!!!!!!!!! Woohoo. Such an exhilarating feeling when something finally works correctly.

Thanks so much for your help!
I hear ya there! You are welcome, glad you got it to work.