简体   繁体   中英

to make that input text field holds focus and not to lose when I click on something else (to focus always be on input with id=“input_message”)?

I made simple web chat, bubles ( messages) above one text field (input message) and send button. How to make that input text field holds focus and not to lose when I click on something else (to focus always be on input with id="input_message") ?

var el = document.getElementById('input_message');

el.focus();

el.onblur = function () {
    setTimeout(function () {
        el.focus();
    });
};

Here's the fiddle: http://jsfiddle.net/MwaNM/

Here's a dirty hack.

<input type="text" id="input_message" />
<script type="text/javascript">
    with (document.getElementById('input_message')) {
        onblur = function(e) {
            var elm = e.target;
            setTimeout(function(){elm.focus()});
        }
        onkeydown = function(e) {
            var key = e.which || e.keyCode;
            if (key == 9) e.preventDefault();
            // code for tab is 9
        }
    }
</script>
var inputElement = document.getElementById("input_message");
    inputElement.focus();
    inputElement.addEventListener("blur", function(event){
        inputElement.focus();
    }); 

http://jsfiddle.net/653w1mpv/

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