0

I have an input box, where i would like a text to disappear. After googling around i found a nice solution over here: Fade out text value inside of text field using jQuery

However, for some reason it does not work with my code over here:

$(document).ready(function(){
  var input = $("#textboxid").val()
  input.animate({color:'white'},1000, function(){
    $(this).val('').css('color','black');
  }
});

Could you tell where i'm mistaken? thank you

EDIT: The text should disappear after user typed something in.

Community
  • 1
  • 1

2 Answers2

1

val() gets the value of the textbox, that is what's written inside it. It's a string. You can't animate a string, you can animate a jQuery object.

Don't take $("#textboxid").val(), just take $("#textboxid").

EDIT :

I made it simply using just CSS3. This does not require any library and is hardware accelerated.

$("button").click(function() {
    $("input").css("color", "white");
});
input {
  -webkit-transition: all 1s linear;
  transition: all 1s linear;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input value="some value"/>
<button>Fade it away</button>
Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
1

There are some syntax errors in your script:

$(document).ready(function () {
    var input = $("#textboxid"); //you have to use the object here and not the value-string
    input.animate({
        color: '#FFF'
    }, 1000, function () {
        $(this).val('').css('color', 'black');
    }); //the bracket was missing here
});

Demo

Furthermore jQuery don't support the animation of the color-attribute. See here for more information.

See this demo for the animation with the included jQuery-UI Library.

Reference

.animate

Community
  • 1
  • 1
empiric
  • 7,825
  • 7
  • 37
  • 48