I want to overwrite the outerHTML of a few links with the same class name. I just wanted to know if this is not working because jQuery doesn’t support .outerHTML. If it does, may you reference it? Also, is there a way to make this work:
$('.t').each(function() {
$('.t').outerHTML = '<a href="http://www.google.com">' +
'<img src="' + $('.t').attr('src') + '" class="' + $('.t').attr('class') + '" /></a>';
});
It looks like you’re just trying to wrap an
<a>element around each.t. jQuery already has a method for doing this called.wrapYou can read the documentation at https://api.jquery.com/wrap/
An example of usage with your question would be:
If you think you really need to set an element’s outerHTML, you might want to try using the snippet I wrote yesterday as an answer to another question. It mimics the behaviour of
.html()but allows you to get or set theouterHTML()instead.