views:

42

answers:

2

Hi guys,

I have a div on my page where I would like to display comments stored in a database. now what i want to understand is how the jquery live() function works. if I bind a click to an element say the div, then do I have to click? I just want the comments to be drawn and shown when the page loads or reloads.

+1  A: 

live() works through event delegation (some articles you might want to read on event delegation [1][2]).

If you want to show comments on page load / reload, then you just need to put code in $(document).ready() to do that. For example,

$(document).ready(function() {

    // code here to get data from database

});

or the shorthand

$(function() {

    // code here to get data from database

});

No event handler is needed, unless you want to give the user the opportunity to interact with the <div> to say, update with new content from the database.

Of course, you could handle this server-side instead of client-side too.

Russ Cam
+1  A: 

If you only want to retrieve the comments when a page loads, then you don't need to involve any other event then load, which can be done in the main jquery code (assuming you have everything wrapped in an onload function).

so something like:

$(function() {

$(#commentdiv).load("getcomments.php");

});

where getcomments.php is a script that gets the comments and returns them in the html format you want the comments to be in within the div.

Anthony