Skip to content Skip to sidebar Skip to footer

How To Always Focus Keep Focus In Text Box

I have created a single HTML page that contains two divs. The div on the left (90% of page) is the target for ajax results. The div on the right (10% of page) contains a single tex

Solution 1:

Using jQuery would look something like this:

$(function() {
     // Focus on load
     $('.scanner').focus();
     // Force focus
     $('.scanner').focusout(function(){
         $('.scanner').focus();
     });
     // Ajax Stuff
     $('.scanner').change(function() {
         $.ajax({
             async: true,
             cache: false,
             type: 'post',
             url: '/echo/html/',
             data: {
                 html: '<p>This is your object successfully loaded here.</p>'
             },
             dataType: 'html',
             beforeSend: function() {
                 window.alert('Scanning code');
             },
             success: function(data) {
                 window.alert('Success');
                 $('.objectWrapper').append(data);
             },
             // Focuscomplete: function() {
                 $('.scanner').val('').focus();
             }
        });
    });
});

jsFiddle example: http://jsfiddle.net/laelitenetwork/fuMYX/5/

P.S: jQuery haters gonna hate ;).

Solution 2:

Use setInterval :

setInterval(function(){
 var focusbox;
 focusbox = document.getElementById("part_to_search");
 focusbox.focus();
});

Jsfiddle http://jsfiddle.net/g9YxB/10/

Post a Comment for "How To Always Focus Keep Focus In Text Box"