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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:02:55+00:00 2026-06-14T15:02:55+00:00

I have a for loop which generates few ‘a’ tag buttons and few divisions.

  • 0

I have a for loop which generates few ‘a’ tag buttons and few divisions. I am trying to write a function in which with a toggle effect. If I click on a specific a tag only the corresponding div tag should get toggled. I am trying to pass a dynamic value to this function but it doesn’t work. Here is the code. Please help. Thank you.

<html>
<header>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="layout.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="jquery.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js" type="text/javascript"></script>
<style type="text/css"> 
.hideonly
{
padding:50px;
display:none;
}
</style>
</header>
<body>
<?php
$num_row = 5; //dynamic value
$i = 0;
$header_title = "";
    for ($i=0;$i<$num_row;$i++)
      {
        $header_title = $i; // Displaying all the headers and menus for the hotel.
        echo "<div>";
        echo "<br>";
        echo "<a class = \"menu-header-2\" href=\"#\">$header_title</a>";
        echo "<div id = \".$i.\" class = \"hideonly\" >Hello World!</div>";
        echo "<br>";
        echo "</div>";
      } 
?>
</body>
<script> 
$(document).ready(function(){
  $(".menu-header-2").click(function($i){
    $("#" . $i).slideToggle("fast");
  });
});
</script>
</html>
  • 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-14T15:02:56+00:00Added an answer on June 14, 2026 at 3:02 pm

    This solution should work for you.

    $(this).next('div').slideToggle('fast');

    There are some things that you are doing wrong.

    1. Starting an id with a number. In fact your id is the number. It should start with a letter
    2. Having the script after the body. Typically it should be either in the head element or just before body ends
    3. In the .click() function, you are passing $i. It makes no sense. You should be using event instead. In this case, you don’t really need it and can have the method without passing anything to it. This is for times when you would use the event parameter.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a loop in razor which generates a template a number of times.
I have a loop which basically calls this every few seconds (after the timeout):
I have a for-loop which performs the following function: Take a M by 8
I have the following loop which is giving me problems $(#divResults).append('<table>'); $.each( results.d, function(
I have the following loop which generates a little snippet of html: for (var
I have a very basic random number function which generates a random number from
I have the Loop which generates tasks. Code: Task task = null; foreach (Entity
I have a few divs which are generated automagically from a php loop. <div
I have this loop which generates a vector Diff. How do I place the
I have php code on a page which generates a series of buttons, like

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.