Pedro Lobito Pedro Lobito - 1 year ago 120
HTML Question

Change a target attribute with jQuery

I'm trying to add

to every
tag after appending some content to the dom but I'm not being successful.

Please run the snippet and click "Go CORS Go", wait until the content is fully loaded and try to click on the duck. Despite being an
tag, the new attribute doesn't seem to work. Any insights ?

var url = encodeURIComponent("");
$("button").click(function() {
$.getJSON('' + url + '&callback=?', function(data) {
var cors = data.contents;
$('body a').each(function() {
$(this).attr('target', "_blank");

<!DOCTYPE html>
<base href="" />
<script src=""></script>
<button>Go CORS Go</button>

Answer Source

Edit the ajax result instead of the result of the page after the append

try the following:

  var newcors = $(cors).find('a').attr('target', "_blank").end();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download