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

  • Home
  • SEARCH
  • 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 8667793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:03:38+00:00 2026-06-12T18:03:38+00:00

I generate dynamically a toc for elements of class=faqQuestion. The answer resides in a

  • 0

I generate dynamically a toc for elements of class=faqQuestion.
The answer resides in a class=faqAnswer element which is hidden by default.
By clicking on class=faqQuestion entry it will show up with

$(this).next(".faqAnswer").slideToggle(300);

Everything works as expected.

What I want: by clicking on a toc link i will jump to the target faqQuestion element and show the corresponding faqAnweser element.

The way I generate the toc:

$(document).ready(function(){
  var url = window.location.pathname;
  $('<ol />').prependTo('#toc')
  $(".faqQuestion").each(function(i) {
    var current = $(this);
    current.attr("id", "entry" + i);
    $("#toc ol").append("<li class=\"faqToc\"><a id='link" + i + "' href='" + url + "#entry" +
      i + "' entry='" + current.attr("tagName") + "'>" + 
      current.html() + "</a></li>");
  });

This is what I tried, which will jump to the selected faqQuestion but the faqAnswer element is still hidden.

  $(".faqToc").click(function(event){
  $(this).next(".faqAnswer").slideToggle(300);
  });

My problem is this – at least I think so – so I tried something like – which results in “undefined”

    var url = $(this).prop("href");
    alert(url);

Trying attr instead of prop returns also “undefined”.

Can you point out my problem?

I’m trying to improve my Javascript and jQuery know how, so I don’t want to use a toc-plugin.

Update: HTML looks like this:

<div id="toc">
<ol>
<li class="faqToc">
<a href="/faq/#entry0">...</a>
</li>
<li class="faqToc">
<a href="/faq/#entry1">...</a>
</li>
</div>


<p id="entry0" class="faqQuestion">...</p>
<div class="faqAnswer" style="display: none;">...</div>

<p id="entry1" class="faqQuestion">...</p>
<div class="faqAnswer" style="display: none;">...</div>
  • 1 1 Answer
  • 0 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-06-12T18:03:40+00:00Added an answer on June 12, 2026 at 6:03 pm

    A very simple way would be to use the index() method since relationship between the TOC elements and the question/answer elements is 1 to 1.

    $(".faqToc").click(function(event){
      var index=$(this).index(); /* zero based index position of element within it's siblings*/
     /* toggle answer element with same index */
      $(".faqAnswer").eq(index).slideToggle(300);
    });
    

    jQuery API Reference : index()

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

Sidebar

Related Questions

I generate markup dynamically and id for inputs elements: <div class=wrap_input fancy_form style=margin-bottom: 10px;
I dynamically generate png files which after clicking generate button appears under link. generate
How do I count dynamically generate elements with the same class in jquery? I
I need to get access to element which was generate dynamically in jQuery. Thing
I have a variable from which I need to dynamically generate nodes <xsl:template match=banner_discount_1
I'm using JavaScript to dynamically generate a dialogue box (it's a div element), containing
I have a report page which dynamically generate the record and saved in the
I need to dynamically generate radio or checkbox by jQuery. I use the following
I am trying to dynamically generate an XSLT document from an XSLT stylesheet. In
Is there any way to dynamically generate an swf file? Or insert variables after

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.