DavidDunham DavidDunham - 1 year ago 61
Javascript Question

Jquery each through <a> gives me href only

I have a simple menu

<div class="nav-container desktop">
<a href="_index.php?page=_sub_papa&main=klassen">One</a>
<a href="_index.php?page=_sub_papa&main=tw">Twp</a>

I am looping through this with jQuery each and then creating
tags with the complete

$.each( $('.nav-container>a'), function(i, item){
var menupunkt = '<li>' + item + '</li>'
console.log(i, menupunkt);

Examples: http://codepen.io/anon/pen/bwbgap, https://jsfiddle.net/86g44ssp/

In my console I see only the following


enter image description here

Why don't I get the whole a? Because when I just print "item" I get the whole

Answer Source

I have updated your fiddle, you just need to change

 var menupunkt = '<li>' + item + '</li>'


 var menupunkt = '<li>' + item.outerHTML + '</li>'

And there is no need to add extra wrapping or stuff, just get the HTML from outerHTML and you're done !

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download