I have a menu with the following menu structure:
<nav>
<ul>
</ul>
</nav>
With this function I'm trying to copy all titles from my page to the menu.
$('.pagetitle').each(function() {
var titles = $(this).contents().clone();
$('nav ul').html('<li><a>' + titles + '</a></li>');
});
It works just fine without adding the li and a tags but predefining the html tags in my menu, but I have to create them thru this function since I can't know how many titles there will be on the page.
I have read this post: jQuery each returns [object Object] and got a little wiser, but it did not solve my problem. If anyone could explain me why this is happing, that would be great!
.pagetitleelements look like?titlesis a jQuery object with string representation equals to[object Object], maybe you want instead:var titles = $(this).contents().clone().prop('outerHTML');orvar titles = $(this).contents().clone().html();. You just forgot in question to tell what you are looking for.wrap()to wrap the contents with new containers.<h3>tags with content.