I'm a novice in jQuery and I got stuck whit a problem for a while now. I am trying to discover all the links to an external page and add a target="_blank" attribute to them. So far I have created the following script:
$(document).on('load',function() {
$('a').each(function () {
if (location.hostname !== this.hostname) {
$(this).attr('target', '_blank');
}
});
});
This works fine on the elements that are loaded on the page in the first instance, but when new elements are added dynamically it doesn't fire again to add the target attribute. I have tried to change the "ready" function to "on('load')" but to no success.
Any help is highly appreciated and I am grateful for the people who take a second to answer silly questions like this.
Edit: I created and infinite scroll script that loads more elements through an AJAX call. This elements contain "a" tags and I would like the script to run again for them.