views:

710

answers:

6

I have many textfields that show instruction text within the textbox (how the default value would look). On focus the color of the text becomes lighter, but doesn't go away until text is entered. When text is erased, the label returns. It's pretty slick. A default value doesn't cut it, because these go away onfocus.

I have it working, but the code is complicated because it relies on negative margins that correspond to the individual widths of the textfields. I want a dynamic solution where the label for its textfield is positioned correctly automatically, probably using a script.

Any help would be greatly appreciated. But I am not looking for default values as a solution.

Thanks.

Mike

Edited to be more precise.

Edited again to provide some simple code that illustrates the effect I am after:

<input style="position: relative; width: 150px; font-size: 10px; font-family: Verdana, sans-serif; " type="text" name="name" id="name"
onfocus="javascript: document.getElementById('nameLabel').style.color='#BEBEBE';"
onkeypress="javascript: if (event.keyCode!=9) {document.getElementById('nameLabel').innerHTML='&nbsp;';}"
onkeyup="javascript: if (this.value=='') document.getElementById('nameLabel').innerHTML='Your&#160;Name';"
onblur="javascript: if (this.value=='') {document.getElementById('nameLabel').style.color='#7e7e7e'; document.getElementById('nameLabel').innerHTML='Your&#160;Name';}"/>
<label id="nameLabel" for="name" style="position: relative; margin-left: -150px; font-size: 10px; font-family: Verdana, sans-serif;">Your Name</label>
A: 

Do you mean like this? But instead of 'required' it would have the label?

I used a jQuery solution where I set the value of the input to 'required'. The input has a class of gray so the default text is lighter.


Edit after comment

Instead of using focus, you can change the input values on keydown and keyup.

$('.required_input').keydown(function()
{
    if (this.value == 'required')
    {
     $(this).val('').removeClass('gray');
    }
} );

$('.required_input').keyup(function()
{
    if (this.value == '')
    {
     $(this).val('required').addClass('gray');
    }
} );

$('.required_input').blur(function()
{
    if (this.value == '')
    {
     $(this).val('required').addClass('gray');
    }
} );
Emily
Does it set the actual value of the text field to required? It may to be acceptable all the time.
Arun P Johny
I don't think I can use a default value to get the results I am looking for. I updated the post to be more clear.
Mike
You don't need to delete the default value on focus. You can wait until a keydown event. I changed my code to reflect that.
Emily
A: 

What you are asking here is probably what is called textbox watermark.

For this, you usually don't use a label (control) inside a textfield. Instead you replace the content of the textfield when the real content of the text field is empty with some text using certain CSS property and then remove it once you blur out (w/ additional check to see if the text inside the textbox itself is the same as the watermark text. If it is, blank the field again.)

Try this. It's pretty simple implementation of this using jquery and css.

Jimmy Chandra
Not to be nit-picky, but I prefer to refer to this as cue banner text, not watermark. By definition, a watermark is persistent. Cue banner text, on the other hand, goes away when you focus the text box and stays away when you type in it. Not that it's a great reference or anything, but the Win32 APIs also refer to this feature as cue banner text.I agree that you likely don't want to position a label inside the text box. However, I also despise javascript solutions to this. It would be great if there were a property on the textbox element itself to indicate cue banner text.
dustyburwell
Maybe they'll get around to adding it to the standard :). Namingwise I took it from ASP.NET AJAX Control Toolkit that uses that particular term. I'm thinking similarly you can use a pure CSS implementation of this using :hover and background image, it won't be perfect, but doable.
Jimmy Chandra
A: 

This can be a rough idea

<html>
<body onload="onpageload()">
 <input id="input1" name="input1"/>
 <div id="label1">0000</div>
</body>
<script language="javascript">
 function onpageload(){
  var el = document.getElementById("input1");
  var pos = el.getBoundingClientRect();

  var l1 = document.getElementById("label1");
  l1.style.top = pos.top+"px";
  l1.style.left = pos.left+"px";
  l1.style.zIndex = 9999;
  l1.style.position = "absolute";

 }
</script>
</html>
Arun P Johny
Note: getBoundingClientRect() is an IE only function. More here: http://ejohn.org/blog/getboundingclientrect-is-awesome/
Josh Stodola
Actually it is now supported by FF3. Still marginal support, in the grand scheme of things.
Josh Stodola
This internal tool is Safari 4 only and that code places the label just below the textfield.
Mike
A: 

Here's one that I borrowed from somewhere:

$(function() {

 // Give the textbox a watermark
 swapValues = [];
 $('.your_input_class').each(function(i){
  $(this).val("Please enter xyz");
  swapValues[i] = $(this).val();
  $(this).focus(function(){
   if ($(this).val() == swapValues[i]) {
    $(this).val("").css("color", "#333");
   }
  }).blur(function(){
   if ($.trim($(this).val()) == "") {
    $(this).val(swapValues[i]).css("color", "#ccc");
   }
  });
 });

});

And then for your input box:

<input class="your_input_class" type="text" value="" />

It remembers the value that is stored in it when the page loads (well, I'm setting mine directly in the JS) and it also changes the color when it's focused/not focused.

Cory Larson
+1  A: 

I would have taken a different approach (It's not entirely my idea, but i can't find the source for credit):

1st - Use html5 "placeholder" property.

2nd - Use Modernizr.js to detect support of placeholders in the browser and a simple jQuery script to add support to browsers that doesn't support it.

So, the html will look something like that:

<input type="text" class="placeholder" placeholder="Help Text" />
<textarea class="placeholder" placeholder="Another help text"></textarea>

The css:

.placeholder{color:#ccc;}

And the javascript:

/* Set placeholder for browsers that don't support HTML5 <input placeholder='text'>
 * Depends on Modernizr v1.5
 */
if (!Modernizr.input.placeholder){
    $('input[placeholder], textarea[placeholder]')
        .focus(function() {
            var input = $(this);
            if (input.val() == input.attr('placeholder')) {
                input.val('');
                input.removeClass('placeholder');
            }
        })
        .blur(function() {
            var input = $(this);
            if (input.val() == '') {
                input.addClass('placeholder');
                input.val(input.attr('placeholder'));
            }

        })
        //Run once on load
        .blur();

    // Clear all 'placeholders' on submit
    $('input[placeholder], textarea[placeholder]').parents('form').submit(function() {
        $(this).find('[placeholder]').each(function() {
            var input = $(this);
            if (input.val() == input.attr('placeholder')) {
                input.val('');
            }
        });
    });
}
Tombigel
A: 
<script type="text/javascript">
    window.onload = function(){
        inputs = document.getElementsByTagName("input");
        for(i = 0; i < inputs.length;i++){
            var feild = inputs[i];
            if(feild.type == "text"){
                var label = document.getElementById(feild.id + "Label");
                label.style.left = "-" + feild.clientWidth;
            }
        }
    }
</script>

This bit of script should do what you wanted

Gilsham