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>
This solution should work for you.
$(this).next('div').slideToggle('fast');There are some things that you are doing wrong.
idwith a number. In fact youridis the number. It should start with a letterafterthe body. Typically it should be either in theheadelement or just beforebodyends.click()function, you are passing$i. It makes no sense. You should be usingeventinstead. 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 theeventparameter.