AbhimanuSharma
AbhimanuSharma

Reputation: 453

javascript input.focus() is not working in firefox 23

I am using this code.. Error is showing but focus is not working in firefox. As this code is working in IE, i can't say this code is completely wrong.

<form name="frm" action="search.php" method="post">
<input type="text" name="search" onblur="return check();"/>
<input type="submit" name="submit" />
<strong id="err"></strong>
</form>

I am using this string in external javascript.

This code is in valid.js

function check()
{
 var item=frm.search;
 var errr=document.getElementById('err');

 if(item.value.length<3)
 {
 item.focus();
 errr.innerHTML="Entered String is very short";
 return false;
 }
}

Please reply me as soon as possible.

Upvotes: 0

Views: 3161

Answers (3)

Joe Simmons
Joe Simmons

Reputation: 1838

var item = document.getElementsByName('search')[0];

Upvotes: 0

Sunny
Sunny

Reputation: 119

try this one

function check()
{
var item = document.forms['frm'].elements['search'];
 var errr=document.getElementById('err');

 if(item.value.length<3)
 {
 errr.innerHTML="Entered String is very short";
    setTimeout(function() {
    item.focus()
}, 10);
 return false;
 }
}

demo jsfiddle http://jsfiddle.net/ff4vW/

Upvotes: 1

Voonic
Voonic

Reputation: 4775

Its good if you use document.getElementById()

But if you are using name Then you should use

 var item = document.forms['frm'].elements['search'];

Upvotes: 0

Related Questions