Solved

jquery question

Posted on 2013-11-22
8
149 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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 42

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
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…

705 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

19 Experts available now in Live!

Get 1:1 Help Now