jQuery.chatroom = function () {
// load messages on page entry
$.chatData(); // THIS WORKS
// save & load on submit
$('input#submit').click( function(event) { // THIS DOES NOT WORK
event.preventDefault();
var name = $('input#name').val(); // get name
var message = $('textarea#message').val(); // get message
if (name != '' && message != '') {
$.post("php/save.php", { name: name, message: message } ); // save message
$.chatData();
$('textarea#message').val(''); // reset type area
}
else {
$('div#response').empty();
$('div#response').append('<img src=\'images/close.png\' alt=\'Close\'><p class=\'whiteText\'>You must fill your name and message</p>');
$('div#response').fadeTo('fast', 0.7).slideDown(500);
// close response/feedback box
$('div#response img').click( function(event) {
$('div#response').slideUp(400);
});
}
});
}
This will execute chatroom function not when ajax is finished, but when .load function is called. It should without parentheses if input with id "submit" (and other) is available only after ajax.
Sorry for confusion, what I meant was that in your code chatroom was called before ajax.
To call chatroom after ajax call (when it finished) leave out the parentheses after "$.chatroom" and you can also leave out second argument, since it doesn't do anything.
Code which should work:
kaszu wrote:Sorry for confusion, what I meant was that in your code chatroom was called before ajax.
To call chatroom after ajax call (when it finished) leave out the parentheses after "$.chatroom" and you can also leave out second argument, since it doesn't do anything.
Code which should work:
jaoudestudios wrote:why use $.chatroom, instead of $.chatroom()?
The first one is a function pointer (or address) - so you tell jQuery load() how to call your function when the ajax request is completed.
The second one is the trivial function call (i.e. an execution is performed at this line).
jaoudestudios wrote:Also how can I save a value returned from an AJAX call
It's returned as the innerHTML value of div#chatroom element.
There are 10 types of people in this world, those who understand binary and those who don't