• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 162
  • Last Modified:

jquery question

I'm adding a button to all gallery images on a wordpress page.

how can I get this script to apply to each .gallery-icon element on the page.. there may be hundreds in the gallery. Seems to me this should use implicit interation but it only seems to  target the first occurence.  How can I get it to apply to every matching element in the dom?

jQuery(document).ready(function($) {

			
			$(".gallery-icon a:first").after("<a href='"+$(".gallery-icon a:first").attr('href')+"' title='testing' download=''>Hello</a>");
		
});

Open in new window

0
tf2012
Asked:
tf2012
  • 6
2 Solutions
 
Rainer JeschorCommented:
Hi,
can you post a link to your page, so that we can see the actual structure?

Just a guess - not tested:

jQuery(document).ready(function($) {

			
			$(".gallery-icon a:first").each(function () { $(this).after("<a href='"+$(this).attr('href')+"' title='testing' download=''>Hello</a>"); });		
});

Open in new window

0
 
tf2012Author Commented:
just a bunch of test images in there

http://mesotheliomacanada.ca/test-gallery-page/
0
 
tf2012Author Commented:
I'm trying to add a link (in this case 'hello') below the image.   But the loop doesn't iterate through all images, just applies the link to the first one.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
tf2012Author Commented:
you can see it in there now... the hello link should be under each image with the respective href url applied but it doesn't seem to work..

My loop is loopy
0
 
tf2012Author Commented:
so quiet here now.... I guess I'll figure it out on my own, anyone have an idea?
0
 
tf2012Author Commented:
that was easy...

used a:first-child instead of a:first  worked like a charm
0
 
Chris StanyonCommented:
Here you go.

$(".gallery-icon").each(function(i,e) {
   var link = $('a:first', this);
   newLink = $('<a>').attr('href', link.attr('href')).text('Hello');
   link.after(newLink);
});

Open in new window

0
 
tf2012Author Commented:
because its the solution that worked
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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