Jump to content

Copy text from one field to another.


The Little Guy

Recommended Posts

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.