Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

backspace actions 1

Status
Not open for further replies.

WebRic

Technical User
Sep 21, 2004
95
0
0
GB
Hi,

I've a list of full names in a text field. I'd like it so that if a name was partially deleted it would be fully removed.

For example:
joe blogs, john smith, darren peterson,|

if the pipe represented my cursor and I pushed the backspace button once I would be left with

joe blogs, john smith,|

Code:
function detectBack (myField,myFieldValue) {
	if(event.keyCode == 8) {	
		/*Something to remove from string??? */
            myField.focus();
				}
			}

What would I need to do to get this affect?
 
Here is a working example of what you are after. The key here is the lastIndexOf method.
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>Page 1</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<script type="text/javascript">

function detectBack (textObj) {
   if(event.keyCode == 8) {    
      var textValue = textObj.value;
      textValue = textValue.substr(0, textValue.length - 1);
      textValue = textValue.substr(0, textValue.lastIndexOf("|") + 2);
      textObj.value = textValue;
   }
}

</script>
</head>
<body>
<input type="text" id="listStr" value="hello| you | dirty | dog |" onkeydown="detectBack(this)" />
</body>
</html>




[monkey][snake] <.
 
That would sure mess things up if you made a spelling error and backspaces one space to correct it. Billy Bob Adamsom<- would erase the whole name if you backspaced one letter to change the m to an n. You'd also have to add something to save anything after the cursor if you tried to delete a name other than the last one in the list.

If the list is filled in from another source this would be more practical than if the user was typing names in.

Lee
 
Thanks monksnake.

Also, cheers for the advice trollacious, I was hoping to make a ajax messaging system with a similar sort of affect as when your adding the emails in outlook. Your right there would still be a problem with a typo correction, but there are technical reasons I still need it.



 
The email addresses added in Outlook are from a pre-existing list. If you use something like that, you'll probably want to differentiate the way the code handles names that are in the list and those that aren't.

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top