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 113399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:41:46+00:00 2026-05-11T02:41:46+00:00

How do I make the below function more dynamic for example commentLink and commentContainer

  • 0

How do I make the below function more dynamic for example commentLink and commentContainer will have an ID after them like this commentLink-2289 commentContainer-2289 because there will be multiple ones in a list.

Javascript

$(function() {     $('#commentLink').click(function()     {         $('#commentContainer').toggle();         if ($('#commentContainer').is(':visible'))          {             $('#commentContainer').load($(this).attr('href'));         } else          {              $('#commentContainer').html('Loading...'); //Or just leave it as is...         }         return false; //Prevent default action    }); }); 

Html

<div id='SystemNews'>  <ul id='dc_news'>    <li>      <a href='/Home/SystemNews/69' id='commentLink-0'>Comments</a>      <div id='commentContainer-0' style='display:none;'>    Loading...</div>    </div>    </li>    </ul>  </div> 

Note: Please provide a working example, I learn best by example thanks

  • 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. 2026-05-11T02:41:47+00:00Added an answer on May 11, 2026 at 2:41 am

    Try this:

    jQuery(function($){    $('a[id^='commentLink-']').click(function(){     match = this.id.match(/commentLink-(\d+)/);     container = $('div#commentContainer-'+match[1])     container.toggle();     if (container.is(':visible')) {         container.load($(this).attr('href'));     } else {         container.html('Loading...');     }     return false; //Prevent default action   }); }); 

    But It would help if you can post the html or a link to the html so we can see what the format you are working with is. I have no idea if the error you are getting is from my code or yours (since my code is just a mod of yours). Which line number is the error on in the script?


    Edit My original code had a bug with the this.id.match line, that’s been fixed above, try it now.


    Edit : Also, there is an extra closing </div> in your provided HTML, not sure if that’s a typo or not, but it should be removed.

    The click event should be wrapped in $(document).ready(function(){ … }); so that jquery can accurately access the dom (since you are querying for <a> and <div> elements). I’m getting a problem where the last return false; is not being reached, I suspect it has something to do with the is(:visible) code. What is the intended function of that if/else block?

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

Sidebar

Ask A Question

Stats

  • Questions 113k
  • Answers 113k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer you need to change the settings of the sql server… May 11, 2026 at 10:09 pm
  • Editorial Team
    Editorial Team added an answer The three previous answers all mention the problem of a… May 11, 2026 at 10:09 pm
  • Editorial Team
    Editorial Team added an answer We had an identical problem on a project here recently.… May 11, 2026 at 10:08 pm

Related Questions

I'm trying to create my own error window for the project I am working
Do you know how can I make splint ignore where I declare my variables?
I want to convert function object to function. I wrote this code, but it
I have code to create another row (div with inputs) on a button click.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.