0

iv got a really simple javascript question. Ill be using query for parts of it here but there are akin ways of doing it via javascript. basically, I'm writing a little script that makes it so when you click a text box with a value in it, it will take out the value so your can type (ex for most username boxes they have a little note in there). there are probably much better ways of doing this (i can already think of some) so feel free to suggest them as well. anyways I got that part running easily, the problem is that whenever a user clicks again all the data is removed, so if they just want to adjust something they can't. to solve this (ill show code in sec) i put a check variable and an if. this is what it looks like. (it doesn't work, btw)

var unumber = 0;
var pnumber = 0; 

 if(unumber<1){
  $('#username').click(function(){
  unumber = 1;
  $('#username').val('');
 });
 };

 if(pnumber<1){
   $('#password').click(function(){
   $('#password').val('');
   pnumber = 1;
   });
 };

what I'm assuming happens is that every time some one clicks the variables are reset, and this leads to a more general question if this is this case, why would the whole script, not just the event handler, run? Im new to javascript so forgive me if this is a stupid question. Anyways, this is a really simple script and there are better and more efficient ways to do it, but how can it be done this way?

roozbubu
  • 1,106
  • 4
  • 14
  • 30
  • As a quick tip, what you are describing seems to be what is referred to as "watermark" functionality. I recommend having a quick peruse of available watermark plugins for jQuery to see if there isn't a much better solution you can just use. – GregL Nov 16 '11 at 00:06
  • Could you explain why you 'need' to implement it like this? Because like you say it can be done much better/efficiently – Rob Nov 16 '11 at 00:23
  • hmm, did i say i did? If so, I don't, not at all. I was wondering what mistake I made, i really didn't phrase this question correctly – roozbubu Nov 16 '11 at 00:33

3 Answers3

4

Your check for number less than 1, should be within the click handler

var unumber = 0;
var pnumber = 0; 

$('#username').click(function(){
  if(unumber<1){
    unumber = 1;
    $('#username').val('');
  }
});

$('#password').click(function(){
  if(pnumber<1){
     pnumber = 1;
     $('#password').val('');
  }
});

Note that this is not very robust, it doesn't handle tabbing into the fields. To fix that, handle the focus event.

Another problem is that you don't get the message back if you don't type anything and leave the field. A better approach is to compare against the initial value when the field receives focus. If there's nothing in there when you leave the field, restore to the original value.

Here's a jQuery plugin for creating these placeholders: https://github.com/mathiasbynens/Placeholder-jQuery-Plugin

Also, newer browsers support a placeholder attribute that does exactly that http://www.paciellogroup.com/blog/2011/02/html5-accessibility-chops-the-placeholder-attribute/

Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • thanks! wow i don't know why i didn't see that, and yeah the method is definately really bare and undynamic, i was just trying to spot the stupid mistake in my code. so, just to be sure, javascript handlers don't run the entire script again right? just the handler function? – roozbubu Nov 16 '11 at 00:29
  • Be wary of the placeholder attribute it is not fully supported across all browsers. There's a link in my answer with a jQuery implementation to make it function correctly in all browsers – Rob Nov 16 '11 at 00:30
  • and the web app I'm working on is for my school, which for some reason decides to strictly use IE6. :( – roozbubu Nov 16 '11 at 00:30
  • You will find that often government and education have policies that make upgrading their systems time consuming and expensive – Rob Nov 16 '11 at 00:32
  • yeah thats exactly the case, thats just such an annoying browser to work with, but they also allow firefox. It also has to do with the software of the security provider cisco. Do you think you can answer my question in the first comment? It was directed towards juan, but if you could answer it be great – roozbubu Nov 16 '11 at 00:35
  • @roozbubu: I don't understand your question: " javascript handlers don't run the entire script again right? just the handler function?". Think about it for a second, read the code. Of course the whole script is not going to be run again when you click. What's going to be run when you click is the function that is passed to `$('selector').click()` – Ruan Mendes Nov 16 '11 at 18:14
  • Yeah, thought so. Common sense says yes, but when it comes to proggramming you can never be sure, and i thought since javascript is run by the browser there was some sort of weird exception. guess not! sorry, I know it was probably a stupid question. – roozbubu Nov 17 '11 at 01:09
0

I'd say the best way of doing this is to have a clear button inside the text input. See here for an example : How do I put a clear button inside my HTML text input box like the iPhone does?

Community
  • 1
  • 1
pradeek
  • 21,445
  • 2
  • 31
  • 32
0

No need for the messy number checking, use data on the element in question

This should work for you.

$('#username,#password').focus(function(){
  if(!$(this).data('seen')) {
    $(this).data('seen',true);
    $(this).val('');
  }
});

http://jsfiddle.net/DeZ7D/

You could store the placeholder and replace back on blur if the user hasn't entered anything. more detailed implementation here:

http://www.hagenburger.net/BLOG/HTML5-Input-Placeholder-Fix-With-jQuery.html

Rob
  • 7,039
  • 4
  • 44
  • 75
  • This does what the OP's code was trying to do, but doesn't tell him what was wrong with the code, which was the point of the question. Also, suggesting this is a poor choice. This does not handle many cases, listed in my answer. – Ruan Mendes Nov 16 '11 at 00:12
  • thanks for your comments @JuanMendes like you said in your rewrite, it's not very robust – Rob Nov 16 '11 at 00:15
  • thanks! I'm accepting juans answer because he showed me the incredibly dumb mistake i made in my code, but appreciate it too – roozbubu Nov 16 '11 at 00:26