2

I'm using the following code to show and hide a label on a text field when someone enters text in the box.

What I'm attempting to do is when you click on the box the text disappears, and if you don't enter text, the label reappears. Any idea what i'm doing wrong?

<script type="text/javascript">
    $(document).ready(function () {
        $("input:text").focus(function () {
            if ($(this).val == "Name/Email:") {
                $(this).val("");    
            };  
        });

        $("input:text").blur(function () {
            if ($(this).val == "") {
                $(this).val("Name/Email:"); 
            };
        });
    });
</script>

<input id="page4-input1" type="text" value="Name/Email:"  />
<input id="page4-input2" type="text" value="Name/Email:" />
<input id="page4-input3" type="text" value="Name/Email:" />
<input id="page4-input4" type="text" value="Name/Email:" />
<input id="page4-input5" type="text" value="Name/Email:" />
1
  • When clicking on the input boxes, nothing occurs and the label stays in the box Commented Jan 11, 2012 at 11:29

2 Answers 2

1
$(this).val

should instead be:

$(this).val()

since it is a method (and not a property).

Your code, fixed.

Also, consider using the 'placeholder' attribute and falling back to JavaScript in browsers which do not support it.

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

1 Comment

Perfect, i shall have to remember that for next time! Ill give you the accepted answer when i can (timer and all).
0

You missed the brackets after val when performing the comparison:

$("input:text").focus(function () {
    if ($(this).val() == "Name/Email:") {
        $(this).val("");    
    };  
});

$("input:text").blur(function () {
    if ($(this).val() == "") {
        $(this).val("Name/Email:"); 
    };
});

Example fiddle

Comments

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.