jquery clear input default value

Jquery

Jquery Problem Overview


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>

Jquery Solutions


Solution 1 - Jquery

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';

Solution 2 - Jquery

$(document).ready(function() {
  //...
//clear on focus
$('.input').focus(function() {
    $('.input').val("");
});
   //clear when submitted
$('.button').click(function() {
    $('.input').val("");
});

});

Solution 3 - Jquery

$('.input').on('focus', function(){
    $(this).val('');
});

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

Solution 4 - Jquery

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" />

Solution 5 - Jquery

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);
  });

Solution 6 - Jquery

Just a shorthand

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionviktorView Question on Stackoverflow
Solution 1 - JqueryKaustubhView Answer on Stackoverflow
Solution 2 - JquerySashaView Answer on Stackoverflow
Solution 3 - JqueryFaustView Answer on Stackoverflow
Solution 4 - JqueryDJ ForthView Answer on Stackoverflow
Solution 5 - JquerySébastien - VélhostView Answer on Stackoverflow
Solution 6 - JqueryToni Michel CaubetView Answer on Stackoverflow