Page 1 of 1

Issue with .focus()

Posted: Tue Jun 03, 2014 2:10 am
by Tim
Hi all,

It has been awhile since I've posted here. Been working on my game off and on. Working on the chat system right now ... got most of it working ... specifically working on the PM system.

I'm attempting to implement a system where if the user presses the sequence of keys "//" (two forward slashes), it automatically starts the PM syntax to PM the last person you received a PM from.

I am using Mousetrap.js (http://www.paulund.co.uk/handle-keyboar ... usetrap-js) to handle the keystroke sequence event ... that part is working great. If someone types in //, it automatically fills the message field with /<username> which is the correct syntax for sending a PM. My issue is, I also want to focus() on the message field after that is entered so all the user has to do is start typing immediately and it will be

Code: Select all

/<username><space><message>
For whatever reason, if I add the bit of code to focus() on the message field, it adds a trailing slash.

Code: Select all

/<username><space><single forward slash>
If I comment out the focus() part, it looks fine

Code: Select all

/<username><space>
but the user has to manually click on the message field and begin typing.

I have tried a work around and added a bit of code to remove the last character in the message field, but it ends up deleting the space instead (the space before the trailing slash)

Code: Select all

/<username><single forward slash>
If I set that bit of code to remove the last two characters in the message field, it ends up deleting the space and the last character of the username, and the trailing slash stays ...

I have no idea where this trailing slash is coming from!

Any ideas?

Code: Select all

<script src="javascript/mousetrap.js"></script>

<?php
$queryPrivateMessages = mysql_query("SELECT * FROM privateMessages WHERE deliverTo = '$username' ORDER BY id DESC LIMIT 1") or die (mysql_error());
$privateMessagesData = mysql_fetch_array($queryPrivateMessages);

$lastPM = $privateMessagesData['sentFrom'];
?>

<script>
$(document).ready(function() {
  Mousetrap.bind(['/ /'], function(e) {
     var messageBox = document.getElementById('message');

     messageBox.value = "<?php echo '//' . $lastPM . " ";?>";

     messageBox.focus();

     //var strng=document.getElementById('message').value;
     //document.getElementById('message').value=strng.substring(0,strng.length-1)
  });
});
</script>

<form>
  <input type="hidden" id="username" name="username" value="<?php echo $username; ?>" />
  <input type="text" id="message" name="message" placeholder="Message" size="25" maxlength="255" autocomplete="off" />
  <input type="submit" id="send" name="send" onClick="sendMessage()" value="Send" />
  <input type="button" id="history" name="history" onClick="location.href='chatHistory.php'" value="History" />
</form>

<div id="chatMessages">
</div>


Re: Issue with .focus()

Posted: Wed Jun 04, 2014 1:48 am
by Jackolantern
Have you double checked the $lastPM variable to make sure the trailing slash isn't in there from something else?