zimt28
zimt28

Reputation: 705

JavaScript returns [object Object]

I'm trying to get this running

$('#info').replaceWith('<div id="infobox" class="reveal-modal">'+$('#info').contents()+'<a class="close-reveal-modal">&#215;</a></div>');

but It just gives me [object Object]. As I only replace it with $('#info').contents() things work fine.

Upvotes: 3

Views: 1238

Answers (4)

hunter
hunter

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">&#215;</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: "&#215;" }));

Working example: http://jsfiddle.net/hunter/qU4s3/2/

Upvotes: 2

Shankar Cabus
Shankar Cabus

Reputation: 9792

Try:

$(function(){
    var content = $('#info').get(0).outerHTML;
    $('#info').replaceWith('<div id="infobox" class="reveal-modal">'+content+'<a class="close-reveal-modal">&#215;</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

Ankush Jain
Ankush Jain

Reputation: 7049

starts with $('#info').contents(). <-- like this

Upvotes: 0

Musa
Musa

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">&#215;</a></div>');

Upvotes: 1

Related Questions