Szymon Toda
Szymon Toda

Reputation: 4516

How to select value of input onClick?

I have a <input type="text"> and if user clicks inside it I want to make the content (value) of that box selected. How would I do that?

Upvotes: 29

Views: 70192

Answers (4)

Kunal Waghmare
Kunal Waghmare

Reputation: 193

You can try following jQuery code which automatically call on page load

<script>
$(document).ready(function(){
    $('input').each(function(){
        $(this).click(function() {
            this.select();
        });
    });
    $("#return_date").focus();
});
</script>

Upvotes: 0

VisioN
VisioN

Reputation: 145458

Try select method:

document.getElementById("myinput").onclick = function() {
    this.select();
};

Upvotes: 4

Amit
Amit

Reputation: 13384

You can try following code inside a javaScript method, and call the method onClick event of the textbox...

function calledOnClick(){

    document.getElementById('test').select();
}

Upvotes: 2

Ehsan Khodarahmi
Ehsan Khodarahmi

Reputation: 4932

<input type="text" onclick="select()"/>

Upvotes: 76

Related Questions