简体   繁体   中英

using val() with jquery // input text dissappear with jquery

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.

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> 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM