Reputation: 5461
I have wrote onchange event to the text box. in that if the value is less than 10 means i have to focus the cursor to text box for that i tried
function myFunctionId (elt) {
alert(elt.id);
if(elt.value < 10) { alert(elt.value);
document.getElementById("tester").focus();
}
}
http://jsfiddle.net/dRkuv/588/
but that focus is not working. How would i achieve this?
Upvotes: 0
Views: 183
Reputation: 1
You can try this
onblur="myFunctionId(this);"
in text field
function myFunctionId (elt) {
//alert(elt.id);
if(elt.value.length < 10) { alert(elt.value);
elt.focus();
}
}
Upvotes: 0
Reputation: 9777
If I'm not wrong this is what you want in your HTML:
<input type="text" onchange="myFunctionId(this);" id="tester" placeholder="Pass Element">
In the Javscript it should be elt.value.length <10
and you need to setTimeOut to prevent the text cursor from not showing:
function myFunctionId (elt) {
console.log(elt.id);
if(elt.value.length < 10) {
console.log(elt.value);
setTimeout(function(){
elt.focus();
},1);
}
}
Upvotes: 1
Reputation: 830
try this http://jsfiddle.net/shashankreddy09/dRkuv/615/
var interval;
var i=0;
function myFunctionId (elt) {
console.log(elt.id);
if(elt.value.length < 10) { //if ur checkin the sting length if its a number use //elt.value<10
console.log(elt.value);
interval=setInterval(function(){elt.focus();
i++;
if(i>1)
clearInterval(interval); },0);
}
}
Upvotes: 1
Reputation: 712
This is my workaround
function myFunctionId (elt) {
if(elt.value < 10) {
window.setTimeout(function(){
elt.focus();
},0);
}
}
myFunction is called just before elt lost focus so if you call focus on it, it doesn't work. But if you use setTimeout function it would be called a little bit later.
Upvotes: 1
Reputation: 152
function myFunctionId (elt) {
var val=document.getElementById("tester").value;
alert(elt.id);
if(val < 10) { alert(val);
document.getElementById("tester").focus();
return false;
}
}
Upvotes: 1