Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

jquery question

Posted on 2013-11-22
8
Medium Priority
?
160 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 2000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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 44

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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
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)
Suggested Courses
Course of the Month11 days, 8 hours left to enroll

916 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