addio3305
addio3305

Reputation: 63

focus() doesn't working in IE

I tried to use focus() in IE, but it's not working.

This is my source code.

----------------------------HTML----------------------------

<tr>
<td>
    <input type="text" id="INPUT_TEST_1" onblur="fn_onblur(this)"/>
    <input type="text" id="INPUT_TEST_2" onblur="fn_onblur(this)"/>
</td>

----------------------------JavaScript----------------------------

function fn_onblur(obj){
var id = obj.id;
var target = $("#"+id);
if(gfn_isNull(target.val())){
    alert(id+" need input value!!!");
    target.focus();
    setTimeout(function() { 
        obj.focus();
    }, 1);
    return;
}

}

The problem is that when I click INPUT_TEST_1 and INPUT_TEST_2 without any value,

focus() doesn't working, but this works in Chrome and FF.

I already tried

setTimeout(function(){obj.focus();},1);
setTimeout(function(){target.focus();},1);
setTimeout(function(){$("#"+id).focus();},1);
setTimeout(function(){document.getElementById(id).focus();},1);

And etc.

However, anything didn't work for me.

Upvotes: 2

Views: 5090

Answers (2)

Stphane
Stphane

Reputation: 3456

I managed to overcome by setting a timeout to focus into the input

<form>
   <label for="name">Name</label>
   <input id="name" name="name" />
    <br />
   <label for="city">City</label>
   <input id="city" name="city" />
    <br />
</form>

Then ..

$('input[name="name"]').blur(function(e){
    if(/^\s*$/.test($(this).val())){
        alert('Value needed');
        setTimeout(function() {e.target.focus();}, 50);
    }
});

fiddle here

Upvotes: 1

andrea.rinaldi
andrea.rinaldi

Reputation: 1187

Last year I had some problem with onblur Html-attribute on IE.. I found that it only works when tabindex attribute is set.

If you read on MSDN, it's said.

NB: i don't know if IE10 and IE11 have still got the problem.. and we don't know on which version of IE you are working on.

Hope it helps.

Upvotes: 0

Related Questions