0

I can't seem to get this to work: Here's the code:

// Clears the default text on click, but restores if nothing is entered
var clearText =  (function(){
    $("input:text").each(function () {

       var default_value = this.value;

       $(this).focus(function(){
               if(this.value == default_value) {
                       this.value = '';
               }
       });

       $(this).blur(function(){
               if(this.value == '') {
                       this.value = default_value;
               }
       });

    });

    $("textarea").each(function () {

       var default_value = this.value;

       $(this).focus(function(){
               if(this.value == default_value) {
                       this.value = '';
               }
       });

       $(this).blur(function(){
               if(this.value == '') {
                       this.value = default_value;
               }
       });

    });

})();


$('html').on('keydown', 'body', function(e){
    if (e.which == 9)
    {
        clearText();
    }
});

clearText() is definitely being called at page load, but then it doesn't work as a callback function? I'm not entirely sure why, can anyone help me out?

Cheers

2 Answers 2

2

clearText is undefined, it is not a function. You were calling it as you thought you were defining it. Thats what the () at the end of the function assignment was.

var clearText =  function(){
    $("input:text").each(function () {

       var default_value = this.value;

       $(this).focus(function(){
               if(this.value == default_value) {
                       this.value = '';
               }
       });

       $(this).blur(function(){
               if(this.value == '') {
                       this.value = default_value;
               }
       });

    });

    $("textarea").each(function () {

       var default_value = this.value;

       $(this).focus(function(){
               if(this.value == default_value) {
                       this.value = '';
               }
       });

       $(this).blur(function(){
               if(this.value == '') {
                       this.value = default_value;
               }
       });

    });

};

Now the function is invokable

Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way I can make it auto-invoke when it's defined, and also make clearText a function?
Yes there are a couple of ways to do it. But don't, saving one line of code is not worth it being a bit cryptic. Define functions and call them. thats how the human mind works. just add clearText(); right after you define it and everyone will know whats going on.
0

Try to put you functions inside document.ready i.e.

$(document).ready(function(){ 
   // Your functions here... 
   $("input:text").each(function(e){
     // code goes here...
   });
});

No need to wrap it inside another braces/().

1 Comment

Sorry, I probably should have said, what I posted above is a snippet of the code, everything is actually inside $(document).ready

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.