I have a list like the following:
<ul id="links">
<li><a href="/example-url-1">Link 1</a></li>
<li><a href="/example-url-2">Link 2</a></li>
<li><a href="/example-url-3">Link 3</a></li>
</ul>
I'm adding a target
attribute to each link using jQuery, because I don't want to alter the server-side HTML output; it's used in many other places and this is the only instance where the links should have this attribute.
$('#links a').attr('target', '_top');
The problem is that new links are dynamically appended to this list client-side, also using jQuery; when they are, they obviously lack the target
attribute, so I end up with this sort of thing:
<ul id="links">
<li><a href="/example-url-1" target="_top">Link 1</a></li>
<li><a href="/example-url-2" target="_top">Link 2</a></li>
<li><a href="/example-url-3" target="_top">Link 3</a></li>
<li><a href="/example-url-4">Link 4</a></li>
</ul>
I cannot alter the jQuery script which is appending the links in order to include the attribute, so my question is this: I know I can use live()
to automatically set up event handlers for dynamically generated elements, but is there a way of automatically adding attributes to any new elements added?