Reputation: 705
I'm trying to get this running
$('#info').replaceWith('<div id="infobox" class="reveal-modal">'+$('#info').contents()+'<a class="close-reveal-modal">×</a></div>');
but It just gives me [object Object]. As I only replace it with $('#info').contents() things work fine.
Upvotes: 3
Views: 1238
Reputation: 63522
If you're trying to maintain the existing content and place it inside an outer div try using wrap()
and you can call after()
to adding the closing anchor.
$('#info').wrap('<div id="infobox" class="reveal-modal">')
.after('<a class="close-reveal-modal">×</a>')
Working example: http://jsfiddle.net/hunter/qU4s3/
Could also be written like this:
$('#info')
.wrap($('<div>', { id: "infobox", "class": "reveal-modal"}))
.after($('<a>', { "class": "close-reveal-modal", html: "×" }));
Working example: http://jsfiddle.net/hunter/qU4s3/2/
Upvotes: 2
Reputation: 9792
Try:
$(function(){
var content = $('#info').get(0).outerHTML;
$('#info').replaceWith('<div id="infobox" class="reveal-modal">'+content+'<a class="close-reveal-modal">×</a></div>');
})
The $('#info').get(0).outerHTML
returns <div id="info">anything</div>
. But if just want the content, use $('#info').html()
, that returns anything
.
Upvotes: 4
Reputation: 97672
.contents()
returns a jQuery object, what you want is .text()
or .html()
depending on if #info
contains other elements.
$('#info').replaceWith('<div id="infobox" class="reveal-modal">'+$('#info').text()+'<a class="close-reveal-modal">×</a></div>');
Upvotes: 1