简体   繁体   中英

How to select an INPUT element with Javascript

I don't know what its term, it's like when have a non empty HTML input text then while the focus is on before that input's tabindex, you hit the TAB key, then it has a blue selection around the text.

How do you do that with Javascript?

I think you mean "Selection". Look at the javascript DOM functions select() and focus() . eg

<input type="text" id="myTextBox" />

<script type="text/javascript">

    var myTextBox = document.getElementById('myTextBox');
    myTextBox.select();
    myTextBox.focus();

</script>

I thick you would like to apply some css to the the text box. in onfocus say you have a class called as selected for which you give a certain order.

and on blur you remove the class attribute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM