views:

30

answers:

1

Hi I am new to JQuery.

I have a link

<a>href="http://www.ittesters.com&gt;&lt;/a&gt;

I would like to be able to append the following to the end of the href

?iframe=true&width=70%&height=85%

I also need to insert an attribute

rel="prettyPhoto[iframes]

I would like this to happen when the user clicks the link not on page load. Is that possible?

+2  A: 

Most certainly. You can add a click event handler to do it for you:

$('a').click(function(){
    $(this).attr('href', $(this).attr('href') + '?iframe=true&width=70%&height=85%' );
    $(this).attr('rel', 'prettyPhoto[iframes]');
});

You can read more about attr() and click() functions in the jQuery docs.

Pat
Thanks so much! Boy that was such a quick response. Under 1 minute!
Sascha
Some days work is _slooooooow_.
Pat
If you have more than one link on your page it would be a good idea to put an id on it like <a id="itter">. Then instead of $('a') you would use $("#itter'). $('a') will register an event handler to ALL the links in your page.
Elliot Vargas
Instead of creating 3 jQuery objects, you could just create one, change the `attr()` calls, and pass a function to the one for the `href`. `$(this).attr('rel','prettyPhoto[iframes]').attr('href', function(i, href) { return href + "?iframe=true });`
patrick dw
@Elliot agree 100%. I left it as `a` for simplicity in the example. @patrick thanks for the tip - always appreciate learning ways to improve my code.
Pat