The Little Guy Posted July 25, 2007 Share Posted July 25, 2007 OK. this function is supposed to take the title that is typed into the title field, then add the word to the keywords box. it does it, but it does it wired, if you copy the two snippets to a document, then try it, it does something weird, that I can not really explain... please help. <script type="text/javascript"> function addToKeyWords(){ var title = document.getElementById('title').value; var keywords = document.getElementById('key').value; document.getElementById('key').value = ''; document.getElementById('key').value = title+' '+keywords; } </script> <form action="process/uploadGame" method="get"> <p><input type="file" name="swf" /></p> <p>Title:<br /><input id="title" onkeyup="javascript:addToKeyWords();" type="text" name="title" /></p> <p>Keywords (<em>Spererated by spaces, 4 letter minimum words</em>): <br /><textarea id="key" name="keywords"></textarea></p> <input type="submit" name="search" value="Upload" /> </form> Quote Link to comment Share on other sites More sharing options...
micah1701 Posted July 25, 2007 Share Posted July 25, 2007 the problem is its running the function on every key stroke. instead of using onKeyUp, why not use something like onBlur. Then after the user has entered the title, when they click on any other field, it will run your function correctly. <p>Title:<br /><input id="title" onblur="addToKeyWords();" type="text" name="title" /></p> Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted July 25, 2007 Share Posted July 25, 2007 Your other option would be to use a throttle. In the onkeyup event, set a timer to a lambda function that does the actual work with a timeout of 500ms, or half a second. On each keyup event, cancel the timer if it is already present. Essentially what you'd be doing is on every keyup telling JS to run a function half a second from now, but the only way it'd actually run is if the user stopped typing for at least half a second. Quote Link to comment Share on other sites More sharing options...
micah1701 Posted July 26, 2007 Share Posted July 26, 2007 Your other option would be to use a throttle. In the onkeyup event, set a timer to a lambda function that does the actual work with a timeout of 500ms, or half a second. On each keyup event, cancel the timer if it is already present. Essentially what you'd be doing is on every keyup telling JS to run a function half a second from now, but the only way it'd actually run is if the user stopped typing for at least half a second. this method would also work, but personally, i think it might be over kill on the coding side PLUS, what if the user is a slow typer? or takes a few moments to think of how to spell the word? it would cause the same problem you're having now. with the onBlur() method, at somepoint the user will have to click out of the field -- either to type in another field or to click the submit buttom -- and at that point, the function will run. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.