开发者

Fadein a jQuery wrap if input value >1

开发者 https://www.devze.com 2023-03-28 12:08 出处:网络
I have an input with an X (clear) button to clear the value. I want that button to fadein if there is a value in the input.

I have an input with an X (clear) button to clear the value. I want that button to fadein if there is a value in the input.

Here is a fiddle of what I have: http://jsfiddle.net/3m25K/

<input class="inputfi开发者_如何学JAVAeld">
$('input.inputfield').wrap('<span class="deleteicon" />').after($('<span/>').click(function() {

    $('.inputfield').val('').focus();
}));
input{
    font-size:22px;}
span.deleteicon {
    position: relative;
}
span.deleteicon span {
    position: absolute;
    display: block;
    top: -5px;
    right: 7px;
    width: 24px;
    height: 24px;
    background:black;
    cursor: pointer;
}


You could just add a keyup handler and check the if the <input>'s value has transitioned from none to some or some to none and then apply a fadeIn or fadeOut to the button. Something like this:

$('.inputfield').keyup(function() {
    var $span = $('.deleteicon span');
    if(this.value.length > 0 && !$span.is(':visible'))
        $span.stop().fadeIn();
    else if(this.value.length == 0)
        $span.stop().fadeOut();
});

Live version: http://jsfiddle.net/ambiguous/MDnac/3/

0

精彩评论

暂无评论...
验证码 换一张
取 消