This is my php with jquery code:
<?php
require('connection.php');
$query="select title,content from blogs";
echo '<html><head>';
echo '<link rel="stylesheet" href="blog.css" />';
echo '<script src="jquery.js"></script>';
//echo '<script src="blogs.js"></script>';
echo "</head><body>";
$i=0;
if($result=$mysqli->query($query))
{
while($news=$result->fetch_row())
{
echo "<br/><br />"."<strong ><h2 onclick='$(document).ready(function() {
$(this).click(function(){ $(\"#a".$i."\").toggle(\"slow\");});});'>". $news[0]."</h2></strong>"."<br/><br />";
echo "<div id='a".$i."'>".$news[1]."</div>";
$i++;
}
$result->close();
echo "</body></html>";
}
?>
This works but not stable. When I click the first heading it toggles for 2 to 3 times like an animation. When I click the second heading the first and second headings content toggles.This is the function call in the source code of the output php.
<strong ><h2 onclick='$(document).ready(function() {
$(this).click(function(){ $("#a0").toggle("slow");});});'>HELLO WORLD</h2></strong>
Please let me know why it happens like this?I am new to jquery so I use it inline.I know it is a bad practice to do so and eliminates the biggest advantage of using jquery.
$(document).ready, it is not needed.