Solved

jquery question

Posted on 2013-11-22
8
156 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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…

636 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