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);   }); }); 

codepen


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -