Issue with .focus()

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
Tim
Posts: 37
Joined: Fri Jun 10, 2011 12:49 am

Issue with .focus()

Post 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>

User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: Issue with .focus()

Post by Jackolantern »

Have you double checked the $lastPM variable to make sure the trailing slash isn't in there from something else?
The indelible lord of tl;dr
Post Reply

Return to “Beginner Help and Support”