Some template written using jquery is as follows . it is not working . Any suggestions to use jquery efficiently .
<html>
<head>
<script type="text/javascript" src="/jquerycall/"></script>
<script type="text/javascript">
$(document).ready(function()
{
self.setInterval("clock()",1000);
$("button").click(function()
{
clock;
});
function clock()
{
clock();
time=new Date();
var s = "<p>" + time + "</p>";
$(s).appendTo("div");
}
});
</script>
</head>
<body>
<form method="post">
<button type="button">Click Me</button>
<div id="someid"></div>
</form>
</body>
</html>
To summarise:
When you call a function, you need to call it with parentheses and optionally parameters (e.g. functionName(); instead of functionName). This applies to all JavaScript.
Imagine there is one little gnome happily running through your code. When he gets to a line he reads it and does it. So let’s say you’ve called clock(). He gets there and goes “what now? Oh, I have to go to clock()”. So he goes to clock(). He gets there and goes “what now?” Oh, I have to go to clock()”. See the pattern? He’s going to keep doing this forever, but he never actually gets a chance to do the things that you want clock() to do, because the first thing he sees as soon as he reaches clock() is that he needs to go to clock(). This is called infinite recursion. What you really wanted was to say “every so often I want a little gnome to go to clock() and do the things in there” which is what setInterval does.
$(s).appendTo(‘div’) selects all things that match ‘s’ (which will be nothing) and appends them to all things that match ‘div’ (which is every div). Instead, you want to select the div with ‘someId’: “$(‘#someId’)”, and append ‘s’ to that: “.append(s);”. If you don’t want to append but instead replace, you will want to use “.html” not “.append”.
Edits