jquery - Get H3 and build link with ID -
i'm trying h3 elements on page build links , create dynamic summary.
so here's have :
$('h3').attr('id', function() { return this.innerhtml; }); $('h3:not(:last)').append(" › "); var headers = $('h3').text(); $('.summary').append(headers);
i have : header1 › header2 › header3 › header4
but don't know how use id of each h3 elements , build links have :
<a href="header1">header1</a> › ...
thank help.
regards.
edit : problem " › " added in h3 , not in summary...
you should use jquery's .each()
function, this:
$(document).ready(function() { $('h3').each(function(index) { var id = $(this).attr('id'); var text = $(this).text(); var spacer = index ? ' > ' : ''; // don't show spacer first item var header = spacer + '<a href="#' + id + '">' + text + '</a>'; $('.summary').append(header); }); });
Comments
Post a Comment