Reputation: 66488
I want to write a plugin that call replaceWith on this, how can I select that new content so I can chain my plugin for new content.
$.fn.spam = function() {
this.replaceWith('<span>spam</span>');
};
and I want to do something like
$('div').span().css('background-color', 'red');
replaceWith
return old content, how can I get the new content?
Upvotes: 0
Views: 62
Reputation: 66488
I found a way to do this in one line using replaceAll
jquery method
$.fn.spam = function() {
return $('<span>spam</span>').replaceAll(this);
};
Upvotes: 0
Reputation: 318182
$.fn.spam = function() {
var elems = $();
this.each(function() {
var span = $('<span />', {text: 'spam'});
$(this).replaceWith(span);
elems.push(span.get(0));
});
return $(elems);
};
$('div').spam().css('background-color', 'red');
Upvotes: 2