I currently have two parts: an interactive chat, and a couple functions that can dynamically add tabs and remove them again. As you might expect I now want to merge them so that you can have a chat within each tab. The way I'm now trying that is by merging all functions in one script, and upon adding a new tab, simply appending the textarea and conversation-div (to hold messages) that I need to run the chat. The problem is that when I hit the send button it doesn't seem to call the function sendMessage().
So I add the html dynamically is like this:
function addTab() {
$('#pageTab').append(
$('<li><a href="#usertab' + this.id + '">' + this.id +
'<button class="close" type="button">×</button></a></li>'));
$('#pageTabContent').append($('<div class="tab-pane" id="usertab' + this.id
+'"><form id="send_message_form"><textarea id="messageText" rows="1" cols="25"></textarea></br>'
+ '<input type="button" id="sendButton" value="Send Message"></form>'
+ '<div class="conversation"></div></div>'));
$('#page' + this.id).tab('show');
}
and instead of the sendMessage I simplified the click action like this:
$("#sendButton").click(function(){
alert("BEHOLD, I AM ALIVE!!");
});
But unfortunately no alert pops up. Does anybody know how I can get this to work? All tips are welcome!
(a paste of the full code that I have is right here)
[EDIT] Thanks for the help! I now indeed can show the alert button. The strange thing is that when I subsequently call another function from within this one to POST a message to the server, it does nothing.
So I now call sendMessage from within the on("click" like below. Although it now does show the "BEHOLD, I AM ALIVE!!" and also the "BEHOLD, We're in the sendMessage.", Firebug shows that it doesn't do the POST, even though I use this very same message in another page (without the tabs) where it does work. Any ideas what's wrong with this then?
function sendMessage(message, user_id) {
alert("BEHOLD, We're in the sendMessage.");
$.ajax({
type: "POST",
contentType: "application/json",
accepts: "application/json; charset=utf-8",
url: "/api/support/" + "1",
data: JSON.stringify({
'action': 'ADD_MESSAGE',
'user_id': user_id,
'message': message
}),
dataType: "json",
success: function(data) {
alert("BEHOLD, I have posted!!");
updateConversation();
}
});
}
$(".container").on("click", "#sendButton", function() {
alert("BEHOLD, I AM ALIVE!!");
var text = $("#messageText").val(); // Get the text
sendMessage(text, 1);
});