29

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?

3
  • The question is quite broad, I assume you know to use javascript, maybe try and ask about the specifics Commented May 2, 2012 at 12:29
  • a hint: w3schools.com/htmldom/dom_using.asp Commented May 2, 2012 at 12:29
  • In IE its not working properly.. If you already have some value inside input box or any placeholder it is not working.. Can you tell me what to do ? Commented Apr 26, 2016 at 9:33

4 Answers 4

76
<input type="text" onclick="select()"/>
Sign up to request clarification or add additional context in comments.

3 Comments

In IE its not working properly.. If you already have some value inside input box or any placeholder it is not working.. Can you tell me what to do ?
And it's possible to unselect it? My idea is select(); document.execCommand('copy'); unselect();
how to select() if the onClick event is on another element , let's say an <i> tag then how to use select() to copy the value or the placeholder in the <input> tag, PS the 2 elements are in the same <div> tag parent , but the <i> tag , not a child of <input> tag
4

Try select method:

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

Comments

2

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

function calledOnClick(){

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

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.