Solved

jquery question

Posted on 2013-11-22
8
150 Views
Last Modified: 2013-11-27
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
Comment
Question by:tf2012
  • 6
8 Comments
 
LVL 44

Assisted Solution

by:Rainer Jeschor
Rainer Jeschor earned 500 total points
ID: 39669344
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
 
LVL 1

Author Comment

by:tf2012
ID: 39669484
just a bunch of test images in there

http://mesotheliomacanada.ca/test-gallery-page/
0
 
LVL 1

Author Comment

by:tf2012
ID: 39669577
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
 
LVL 1

Author Comment

by:tf2012
ID: 39669586
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 1

Author Comment

by:tf2012
ID: 39670432
so quiet here now.... I guess I'll figure it out on my own, anyone have an idea?
0
 
LVL 1

Accepted Solution

by:
tf2012 earned 0 total points
ID: 39670506
that was easy...

used a:first-child instead of a:first  worked like a charm
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39670513
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
 
LVL 1

Author Closing Comment

by:tf2012
ID: 39680263
because its the solution that worked
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Julian Matz As of jQuery (http://jquery.com/) 1.4 the .live() method (http://api.jquery.com/live/) supports custom events as well as some standard JavaScript events that it previously didn't support. Among these is the submit event. Unlike …
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now