How do I rewrite this script to make it do what I want it to do?
June 1, 2008 9:53 AM   Subscribe

Can you help me? I'd like to use this mootools script on a contact via email form on a site I'm designing for a friend, and I need to know if something about it can be changed.

As it is now, when a trigger word is typed, all the words typed so far in the field disappear and you have to start all over again. How can I change it so that all text typed in the box remains even when Event is triggered? Is it possible? If so and you care to explain, please pretend as if you're talking to a moran.
posted by iconomy to Computers & Internet (5 answers total)
 
See this:
'burn': function(text) {
txt.value = '';
Remove the line: txt.value = '';

It's setting the value to blank.
posted by jdgdotnet at 10:02 AM on June 1, 2008


Response by poster: That partially solves the problem...thank you! Now the text remains in the box like I wanted it to, but the first Event (not sure if that's the right terminology) stays there too, even when I type another trigger word.
posted by iconomy at 10:47 AM on June 1, 2008


Change txt.value='' to txt.value = txt.value.replace(text,'')
posted by missmagenta at 12:13 PM on June 1, 2008


To expand on that a little, the event looks for the trigger words in order, if you retain the content of the text box, when you begin to type the next word, the trigger word is found again, you need to remove the trigger word.

...although I can't for the life of me see a practical application for this script (particulalry in the context of a contact form) - perhaps if you gave more details on what you were tring to achieve, we could be more helpful.
posted by missmagenta at 12:15 PM on June 1, 2008


Probably the cleanest way to do this is to add an endswith function to the String object, which we can use to check if the trigger word is actually the last word in the text.

At the top of the script add:

String.extend({endswith:function(A){return this.substr(this.length - A.length, this.length) == A;}});


Then you need to add this check in the keyup function:

if (txt.value.contains('hello') && txt.value.endswith('hello')) txt.fireEvent('burn', 'hello world!');


This should work as you expect it to with a couple of caveats - the event will occur again if the user backspaces to the trigger word or if the user changes the cursor position and types in the middle of their text while the trigger word is at the end of the string. I can't see any practical way to stop this happening, though.
posted by xchmp at 12:35 PM on June 1, 2008


« Older Any companies that hire PC technicians as...   |   Help identify this horror story mentioned in "The... Newer »
This thread is closed to new comments.