1

I have the following code which is supposed to target the #newsletter input, add the default text into it and then remove it when a user tabs / clicks into the input. However it doesn't seem to work, and I get the following console error:

Uncaught SyntaxError: Unexpected end of input

Any ideas?

$(function() {
  var defaultText = "Your email address";
  $('#newsletter').val(defaultText).focus(function() {
      if (this.value == defaultText) {
          $(this).val('');
      }
  }).blur(function() {
              if (this.value == '') {
                  $('#newsletter').val(defaultText);
              }
  });
1
  • here's the jsfiddle for ur code jsfiddle.net/hkVub Commented Nov 20, 2012 at 12:10

2 Answers 2

4

You are missing closing brackets in your code:

$(function() {
    var defaultText = "Your email address";
    $('#newsletter').val(defaultText).focus(function() {
        if (this.value == defaultText) {
            $(this).val('');
        }
    }).blur(function() {
        if (this.value == '') {
            $('#newsletter').val(defaultText);
        }
    });  // <-- check it here
});

By the way, the thing you try to implement is a placeholder for the form field. It is worth to read about placeholder attribute in HTML5.

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

2 Comments

Cheers! Hadn't heard of placeholder, will check it out!
@arulmr Thanks for edit though I can't consider it as helpful.
2

}); missing at the end of code, so issue is due to unbalanced braces.

here is corrected version

$(function() {
    var defaultText = "Your email address";
    $('#newsletter').val(defaultText).focus(function() {
        if (this.value == defaultText) {
            $(this).val('');
        }
    }).blur(function() {
        if (this.value == '') {
            $('#newsletter').val(defaultText);
        }
    });
});

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.