1
$(".guide").focus(function(){
   $(this).removeClass("guide").val("");
});

I just need to know if there's something else I need to do to free up the event handler, or is it fine?

1
  • May be complement it with .blur() event? Commented Apr 26, 2011 at 5:48

2 Answers 2

1

You can also use jQuery.fn.one for an event handler which is called once and then automatically detached.

$(".guide").one("focus", function(){
   $(this).removeClass("guide").val("");
});
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, I did not know about the one() handler. Cool!
1

Not quite sure what you mean by 'free up the event handler' but I will guess..

Removing the class would work only if you are using a .delegate handler. In your situation where you explicitly attach the handler to elements you need to .unbind the handler from the element.

If you have a number of elements with a class of guide in a form then you will be best using delegate as follows

$('.guide').delegate('form', 'focus', function(){
   $(this).removeClass("guide").val("");
});

2 Comments

Sorry, I figured it out - $(this).removeClass("guide").unbind("focus").val("");
@NeXXeuS Yeah that is what I said!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.