Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • Last Modified:

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
0
nanharbison
Asked:
nanharbison
  • 5
  • 4
1 Solution
 
Tom BeckCommented:
How about something like this:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
	$('img[typeof="foaf:Image"]').each(function(){
		var largeImgSrc = $(this).attr('src');
		//you will need to create your href value for each image here and assign to largeImgSrc
		largeImgSrc = largeImgSrc.substring(0, largeImgSrc.lastIndexOf('.')) + '_large.jpg';
		$(this).attr('rel', 'lightbox[custom]').attr('class', 'myImgClass');
		$(this).wrap('<a href="' + largeImgSrc + '"></a>');
	});
});
</script>

Open in new window

0
 
nanharbisonAuthor Commented:
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.
0
 
Tom BeckCommented:
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
nanharbisonAuthor Commented:
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
0
 
Tom BeckCommented:
No problem. Let me know if both of those suggestions fail.
0
 
nanharbisonAuthor Commented:
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.
0
 
Tom BeckCommented:
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
0
 
nanharbisonAuthor Commented:
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!
0
 
Tom BeckCommented:
I hear ya there! You are welcome, glad you got it to work.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now