There are two jQuery features you want to use here.
1) click
. This will use the anonymous function as the only parameter and will execute it when the element is clicked.
2) html
. This will take the html string as the only parameter and replace the contents of your element with the provided html.
So, in your case, you want to do the following:
$('#content-container a').click(function(e){ $(this).parent().html('<a href="#">I\'ma new link</a>'); e.preventDefault(); });
If you only want to add content to your div, and not replace everything in it, you should use append
:
$('#content-container a').click(function(e){ $(this).parent().append('<a href="#">I\'ma new link</a>'); e.preventDefault(); });
If you want the newly added links to also add new content when clicked, you should use event delegation :
$('#content-container').on('click', 'a', function(e){ $(this).parent().append('<a href="#">I\'ma new link</a>'); e.preventDefault(); });
Joseph Silber Aug 21 '11 at 15:35 2011-08-21 15:35
source share