Jump to content

Recommended Posts

Posted

Just wondering how you guys do this --- say you have a script that adds a couple of links below every image on the page. How do you associate each set of links with their image?

 

I would typical use the "rel" attribute, as my understanding is that this is used to relate one element to another - though it is outside of the HTML 4.01 spec for most elements. So, if I had a link to display the image in a new window, the markup of that link would like this:


The javascript to open a new window would be attached to the element, but would use jQuery's .click() instead of an "onclick=" attribute. The "href=" would be there for compatibility's sake, just in case for some reason the browser let me insert the markup, but didn't recognize the onclick event properly.

Posted

Any particular reason for creating the relationship?

 

If it is so that your hyperlink knows which image to target, assuming you always add your hyperlinks after the image you could use the jQuery .prev() function.

 

$(this).prev('img').attr('href');

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...