Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
code block
Source Link
redsquare
  • 78.8k
  • 21
  • 155
  • 159

Ok first off you should only have one tag with the id of comment since ids are supposed to be unique, but if we go with your code you have to setup a default value first like this:

<textarea id="comment">Enter text here</textarea> 

Now the javascript:

 (function() {  $('textarea#comment').each(function() {   var $txt = $(this).val();   //alert($txt);   $(this).bind('focus', function() {   if($(this).val() === this.defaultValue) {   $(this).val('');   }   });  });   })(); 

})();

The self executing function syntax has been corrected here and should be as follow:

(function () { // my code here })(); 

and for default value use:

this.defaulValue; // works for input boxes as well 

Hope it helps. Cheers

Ok first off you should only have one tag with the id of comment since ids are supposed to be unique, but if we go with your code you have to setup a default value first like this:

<textarea id="comment">Enter text here</textarea> 

Now the javascript:

(function() { $('textarea#comment').each(function() { var $txt = $(this).val(); //alert($txt); $(this).bind('focus', function() { if($(this).val() === this.defaultValue) { $(this).val(''); } }); }); 

})();

The self executing function syntax has been corrected here and should be as follow:

(function () { // my code here })(); 

and for default value use:

this.defaulValue; // works for input boxes as well 

Hope it helps. Cheers

Ok first off you should only have one tag with the id of comment since ids are supposed to be unique, but if we go with your code you have to setup a default value first like this:

<textarea id="comment">Enter text here</textarea> 

Now the javascript:

 (function() {  $('textarea#comment').each(function() {   var $txt = $(this).val();   //alert($txt);   $(this).bind('focus', function() {   if($(this).val() === this.defaultValue) {   $(this).val('');   }   });  });   })(); 

The self executing function syntax has been corrected here and should be as follow:

(function () { // my code here })(); 

and for default value use:

this.defaulValue; // works for input boxes as well 

Hope it helps. Cheers

Source Link
Ady Ngom
  • 1.3k
  • 2
  • 10
  • 12

Ok first off you should only have one tag with the id of comment since ids are supposed to be unique, but if we go with your code you have to setup a default value first like this:

<textarea id="comment">Enter text here</textarea> 

Now the javascript:

(function() { $('textarea#comment').each(function() { var $txt = $(this).val(); //alert($txt); $(this).bind('focus', function() { if($(this).val() === this.defaultValue) { $(this).val(''); } }); }); 

})();

The self executing function syntax has been corrected here and should be as follow:

(function () { // my code here })(); 

and for default value use:

this.defaulValue; // works for input boxes as well 

Hope it helps. Cheers