Jquery .val() for input only works once.

Posted by Bolt_Head on Stack Overflow See other posts from Stack Overflow or by Bolt_Head
Published on 2009-10-22T12:54:22Z Indexed on 2010/04/17 13:13 UTC
Read the original article Hit count: 337

Filed under:

I'm trying to create a chat box for my game.

The user type's their chat into the input:text feild and by ither pressing Enter or clicking the button submits the chat text.

This all works, however for some reason after the first time a user submits a chat message it fails to get the text from the input field.

Here is my code.

$(document).ready(function() {
	$("#chatEnter").live('click',function(){
		var chat = $('#chatText').val();
		sendChat(chat);
	});
});

$(document).ready(function() {
	$("#chatText").keypress(function(e){
		if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13))
		{  
			var chat = $('#chatText').val();
			sendChat(chat);
			return false;
		}
		else return true;   
	});
});

function sendChat(chat)
{	
	alert(chat); //temp test alert
	$.getJSON("includes/boardUpdate.php",{chat: chat, bid: bid});
	$('#chatText').val("");
}

It doesn't matter if i first submit a text by clicking the button or pressing enter, all future attempts submit blank entrys until I refresh the page.

Edit: I've tried it with and without the line to clear the text box, same results both ways.

Your help is appreciated.

© Stack Overflow or respective owner

Related posts about jQuery