0

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>';
});
Andy E
  • 338,112
  • 86
  • 474
  • 445
David G
  • 94,763
  • 41
  • 167
  • 253
  • What is `.t` in this case? Do you mean `.wrap`? http://api.jquery.com/wrap/ – Felix Kling Sep 07 '11 at 15:06
  • I think this post may be what you're after: http://stackoverflow.com/questions/2419749/get-selected-elements-outer-html. – William Niu Sep 07 '11 at 15:10
  • @William: that post refers to getting outer HTML only. This question is looking for a method to set the outer HTML. – Andy E Sep 09 '11 at 11:07
  • possible duplicate of [How to use outerHTML in JavaScript?](http://stackoverflow.com/questions/2483429/how-to-use-outerhtml-in-javascript) – Peter O. Jan 12 '12 at 06:29

5 Answers5

2

It looks like you're just trying to wrap an <a> element around each .t. jQuery already has a method for doing this called .wrap

You can read the documentation at https://api.jquery.com/wrap/

An example of usage with your question would be:

$('.t').wrap('<a href="http://google.com">');



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 the outerHTML() instead.

JGallardo
  • 11,074
  • 10
  • 82
  • 96
Andy E
  • 338,112
  • 86
  • 474
  • 445
0

jQuery offers what you want, just not as a complete 'outerHTML' function:

$('.t').each( function() {
    // Modify the html directly
    $(this).html('<img src="' + $('.t').attr('src') + '" class="' + $('.t').attr('class') + '" /></a>');
    // Use attr to change href
    $(this).attr('href','http://www.google.com');
});

You could also .append() a new $('<img/>') which you have manipulated with .attr() and .addClass() instead of writing out the html string.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Adam Field
  • 101
  • 1
  • 2
0

Creating simple outerHTML jQuery plugin:

(function($) {
  $.fn.outerHTML = function() {
    return $(this).clone().wrap('<div></div>').parent().html();
  }
})(jQuery);


// usage
$("<p>Hello</p>").outerHTML(); //  "<p>Hello</p>"
Maksym Kozlenko
  • 10,273
  • 2
  • 66
  • 55
-1

You could use this in your each!

this.outerHTML

To be cross-browser use this jQuery Plugin:

https://github.com/darlesson/jquery-outerhtml

Darlesson
  • 5,742
  • 2
  • 21
  • 26
binarious
  • 4,568
  • 26
  • 35
-1

A new jQuery outerHTML plugin version was release at https://github.com/darlesson/jquery-outerhtml and it seems to do what you want:

    $(".t").each(function(){

        var $this = $(this),
            // Get the attribute values
            src = $this.attr("src"),
            className = $this.attr("class");

        $this.outerHTML("<a href='http://www.google.com'><img src='" + src + "' class='" + className + "' /></a>");

    });
Darlesson
  • 5,742
  • 2
  • 21
  • 26