Jquery show/hide password

前端 未结 7 745
失恋的感觉
失恋的感觉 2021-02-10 17:33

Below Jquery is working fine but How could I shorten this Jquery? I have to show and hide the password for password and confirmed password? https://jsfiddle.net/c5cvLo54/1/

7条回答
  •  -上瘾入骨i
    2021-02-10 18:27

    you can use only one button to control.

    • js:

    ```

    $('.showOrHide').click(function(e){
        var target = e.currentTarget
        $(target).hasClass('show')?hidePassword($(target)):showPassword($(target))
    })
    function hidePassword(e){
        e.removeClass('show').addClass('hide')
        e.prev('input').attr('type','password')
    }
    function showPassword(e){
        e.removeClass('hide').addClass('show')
        e.prev('input').attr('type','text')
    }
    

    html:

    ```

    
    
    

    ```

    style:

    ```

    .show{
        /*css you want */
        color: blue
    }
    .hide{
        /*css you want */
        color: red
    }
    

    ```

提交回复
热议问题