Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1016027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:29:22+00:00 2026-05-16T10:29:22+00:00

$(‘h2’).each(function() { $(‘UL.chapters_list’).append($(‘<li/>’, {text: $(this).text()})) }); This code grabs all of the <H2> tags

  • 0
$('h2').each(function() {
    $('UL.chapters_list').append($('<li/>', {text: $(this).text()}))
});

This code grabs all of the <H2> tags on the page, then copies each of them to their own <LI> inside of <UL class="chapters_list">, so essentially it creates a list of all the <H2> tags.

I need to wrap each <LI> tag that it creates, with an anchor link, this link should have the text of the <h2> that it grabbed, but replacing the spaces in the <h2> with hyphens '-'

So let’s say my script sees 2 <H2> tags on the page, so it grabs the texts of those <H2> tags and copies them to:

<UL class="chapters_list">
<LI>title number one</LI>
<LI>and number two</LI>
</UL>

I need it to now wrap each of those

  • tags with an anchor link, the anchor link will be the text of the LI(or the H2 it just grabbed) but with hyphens (-) replacing spaces.

    so the above will turn into:

    <UL class="chapters_list">
        <a href="#title-number-one">
            <LI>title number one</LI>
        </a>
        <a href="#and-number-two">
            <LI>and number two</LI>
        </a>
    </UL>
    

    Can anyone help? it shouldn’t be too hard, with the code i’ve provided, thank-YOU.

    • 1 1 Answer
    • 2 Views
    • 0 Followers
    • 0
    Share
    • Facebook
    • Report

    Leave an answer
    Cancel reply

    You must login to add an answer.

    Forgot Password?

    Need An Account, Sign Up Here

    1 Answer

    • Voted
    • Oldest
    • Recent
    • Random
    1. Editorial Team
      Editorial Team
      2026-05-16T10:29:23+00:00Added an answer on May 16, 2026 at 10:29 am

      You are about to generate invalid HTML. The links must be inside the list elements.

      This works:

      $('h2').each(function() {
          var text = $(this).text();
          $('<li/>')
              .append($('<a />', {text: text, href:'#'+text.replace(/\s/g, '-')}))
              .appendTo('ul.chapters_list');
      });
      

      Live example: http://jsfiddle.net/mL2HV/

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
        • Report

    Sidebar

    Related Questions

    This is how I get the tags of a body of text. var tags
    I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
    I have a text area in my form which accepts all possible characters from
    I was writing code for dragging mechanism which invokes to wait for small period
    Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
    Hi if i run my project its indicate this logcat error...how to solve this
    Does anyone know how can I replace this 2 symbol below from the string
    I need a function that will clean a strings' special characters. I do NOT
    I have a reasonable size flat file database of text documents mostly saved in
    I have just tried to save a simple *.rtf file with some websites and

    Explore

    • Home
    • Add group
    • Groups page
    • Communities
    • Questions
      • New Questions
      • Trending Questions
      • Must read Questions
      • Hot Questions
    • Polls
    • Tags
    • Badges
    • Users
    • Help
    • SEARCH

    Footer

    © 2021 The Archive Base. All Rights Reserved
    With Love by The Archive Base

    Insert/edit link

    Enter the destination URL

    Or link to existing content

      No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.