javascript - How to consistently use select() when focusing on a field? Is it possible? -


i have created new form site, labels built form fields - "title" 1 when ask question on site.

you can see here: http://utilitiessavings.co.uk/business-energy/business-electricity/ (scroll down)

i had label disappearing field got focused, decided better leave until user starts typing, because otherwise might forget field for

the problem there caret in label text , it's not clear disappear when start typing.

here code (yes, hacked pieces):

    var keypressed = false;      // each input field title     $('input[title], textarea[title]').each(function() {          // if has no value         if($(this).val() === '' || $(this).attr('title'))         {              // add labeled class             $(this).addclass('labeled');              // set value title             $(this).val($(this).attr('title'));          }          // when focused         $(this).focus(function()         {              // select whole field             $(this).select();              // if value label/title             if($(this).val() === $(this).attr('title'))             {                  // wait keypress                 $(this).keypress(function() {                      // if first key pressed                     if(keypressed == false)                     {                         // clear field , remove labeled class                         $(this).removeclass('labeled').val('');                          // set keypressed true next keypress doesn't clear field again                         keypressed = true;                     }                 });             }         }).click(function()         {              // select whole field             $(this).select();          });          // when undocused         $(this).blur(function() {              // if blank             if($(this).val() === '') {                  // add labeled class , title/label                 $(this).addclass('labeled').val($(this).attr('title'));             }             else             {                 if($(this).val() === $(this).attr('title'))                 {                     $(this).addclass('labeled');                 }               }                // set keypressed false next field             keypressed = false;          });      }); 

you notice have put select() onfocus , onclick still doesn't work. it's strange. please advise if it's needed or if should make stackoverflow.com's ask question box?

any appreciated, , sorry rambling feel it's better give more info not enough :)

thanks all,

drew

as mentioned above, posted link, great solution problem.

http://fuelyourcoding.com/scripts/infield


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -