[removed] cross browser solution for selecting all text inside a textbox on focus

前端 未结 4 1709
一生所求
一生所求 2021-01-12 19:35

I\'m after the following functionality:

  • user clicks on or tabs into a textbox
  • all text in the textbox is selected, unless the textbox already had focu
4条回答
  •  执念已碎
    2021-01-12 19:39

    If you can use jQuery then you can do something like;

    $("#myInputField").focus(function(){
        // Select input field contents
        this.select();
    });
    
    // Add this behavior to all text fields
    $("input[type=text]").focus(function(){
        // Select field contents
        this.select();
    });
    

    Taken from HERE

提交回复
热议问题