$(function () { var index = 1; $('h1, h2, h3').each(function () { $(this).before("<a id='" + index + "'></a>"); $(this).clone().appendTo("#second_div"); index++; }); });