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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:43:06+00:00 2026-05-20T12:43:06+00:00

I have the following jQuery code, which works fine: $(document).ready(function () { $(‘#link1’).click(function ()

  • 0

I have the following jQuery code, which works fine:

$(document).ready(function () {
    $('#link1').click(function () {
    $('.data1').slideToggle('slow');
    });
    $('#link2').click(function () {
    $('.data2').slideToggle('slow');
    });
    /* so on a so forth */
});

link1 is an ID that I have for a div tag around a hyperlink. Data1 is an ID for another div tag for content that I want shown if the user clicks on the hyperlink. My question is, if I have 50 or even 100 hyperlinks and content that goes with each of those, it would be cumbersome to list that code 50 or 100 times with each ID listed separately.

How can I do this programmatically? I tried setting up a for loop, but I probably had the syntax screwed up. I also tried using the each() function.

Any help would be greatly appreciated.

  • 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-05-20T12:43:07+00:00Added an answer on May 20, 2026 at 12:43 pm

    The easiest way would be to define a class for these, and also a better ID. I would say something like this:

    <a href="#" id="link_1" class="toggle_link">Link 1 Text</a>
    <div class="data_1">Fos</div>
    
    <a href="#" id="link_2" class="toggle_link">Link 2 Text</a>
    <div class="data_2">Fos Fos</div>
    

    Then you can easily attach your events like:

    $('.toggle_link').click(function () {
      var name=this.id.split('_');
      $('.data_'+name[1]).slideToggle('slow');
    });
    

    I put the underscore there to make it easy to find where the ID starts. I would not say this is production-ready code yet, but can give you the idea.

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

Sidebar

Related Questions

I have the following jquery code which works fine. $('#onlyTwo').click(function() { $(#textarea3).attr(disabled, true); $(#textarea4).attr(disabled,
I have the following jQuery code which all works fine: $(#sidebar .m3, #sidebar .m5).toggle(function()
I have the following jQuery code: $(body.page-calendar-practices-2012-05 #content-header h1#title) which works fine. I can
I have the following jquery code: $(document).ready(function() { $(body[class*=page-calendar-practices] #content-header h1#title).after(<div id='athletics_practice-schedule'><div id='inner-title'><a href='www.example.com'
I have the following JQuery code: <script type=text/javascript> $(document).ready(function () { var $containerHeight =
I have the following JQuery code: $(document).ready(function () { var $containerHeight = $(window).height(); if
I have the following code, which works fine in FF, but the .append() function
i have the following jQuery code. $(function() { $('.clickme').click(function() { $('.totalthing').slideToggle('slow','easeInOutQuart', function() { //
I have the following code which works fine in all browsers other than IE.
I have the following jQuery code: $('a[rel=close]').click(function() { alert('Close click!'); $('div#purchasePanel').hide(); }); This is

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.