78

How can I clear the default value of an input form onfocus with jquery and clear it again when tha submit button is pressed?

<html>
        <form method="" action="">
            <input type="text" name="email" value="Email address" class="input" />
            <input type="submit" value="Sign Up" class="button" />
        </form>
</html>

<script>
$(document).ready(function() {
    //hide input text
    $(".input").click(function(){
        if ($('.input').attr('value') == ''){
            $('.input').attr('value') = 'Email address'; alert('1');}
        if  ($('.input').attr('value') == 'Email address'){
            $('.input').attr('value') = ''}
    });
});
</script>
4
  • 10
    Could you use the native placeholder attribute instead? There are various polyfills available to make it work in older browsers. Commented Aug 1, 2012 at 8:09
  • @JamesAllardice Why you not posted it as an answer? I think is the easiest and best solution. Commented Aug 1, 2012 at 8:16
  • 1
    @jelies - I didn't post it as an answer because while it does the same thing as what the OP is trying to do, it doesn't really answer what's wrong with their code. But I agree, it is definitely the easiest solution! Commented Aug 1, 2012 at 8:21
  • Yes it is a good answer and I have upvoted it, but I do not want to use the placeholder tag. Commented Aug 1, 2012 at 10:34

7 Answers 7

138

You may use this..

<body>
    <form method="" action="">
        <input type="text" name="email" class="input" />
        <input type="submit" value="Sign Up" class="button" />
    </form>
</body>

<script>
    $(document).ready(function() {
        $(".input").val("Email Address");
        $(".input").on("focus", function() {
            $(".input").val("");
        });
        $(".button").on("click", function(event) {
            $(".input").val("");
        });
    });
</script>

Talking of your own code, the problem is that the attr api of jquery is set by

$('.input').attr('value','Email Adress');

and not as you have done:

$('.input').attr('value') = 'Email address';
Sign up to request clarification or add additional context in comments.

3 Comments

I have decided to use this answer as it seems the most elegant.
thnx.i myself am new to jquery.this is my 1st accepted anser here,and third overall(as you can probably deduce by my reputation points)
Why $('.input').removeAttr('value') does not work?
18
$(document).ready(function() {
  //...
//clear on focus
$('.input').focus(function() {
    $('.input').val("");
});
   //clear when submitted
$('.button').click(function() {
    $('.input').val("");
});

});

2 Comments

This is wrong $('.input').attr('value') = 'Email address';, should be $('.input').attr('value', 'Email address'); how is this answer upvoted?
@ToniMichelCaubet Edited the answer, thank you. That code wasn't related to the question anyway. I copied it from topicstarter's code.
10
$('.input').on('focus', function(){
    $(this).val('');
});

$('[type="submit"]').on('click', function(){
    $('.input').val('');
});

Comments

8

Unless you're really worried about older browsers, you could just use the new html5 placeholder attribute like so:

<input type="text" name="email" placeholder="Email address" class="input" />

2 Comments

"Unless your really worried about older browser"... you can polyfill the placeholder attribute so it works in older browsers too. See my comment on the question, and see the Modernizr wiki for other examples.
Yeah that's true, or you could just write a bit of JQuery to pull the attribute yourself if your keen.
3

Try that:

  var defaultEmailNews = "Email address";
  $('input[name=email]').focus(function() {
    if($(this).val() == defaultEmailNews) $(this).val("");
  });

  $('input[name=email]').focusout(function() {
    if($(this).val() == "") $(this).val(defaultEmailNews);
  });

Comments

2

Just a shorthand

$(document).ready(function() {
    $(".input").val("Email Address");
        $(".input").on("focus click", function(){
            $(this).val("");
        });
    });
</script>

Comments

0

Here is the smallest and easiest way to clear a html input field using jQuery

$('#inputFieldId').val('');

That's it !

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.