Focus Button From Javascript Withour Clicking It
I call element.focus(); Where element is HTMLInputElement of type=button. But then the browser clicks the button! That's in mozilla and chrome. How do i highlight the button with
Solution 1:
No .focus()
doesn't click the button or submits the form: http://jsbin.com/onirac/1/edit
It does exactly what you want it to.
Solution 2:
Well, i've identified the reason. I was handling the onkeydown event for Enter key. The solution is to use e.preventDefault();
functionConvertEnterToTab(s, e, numSkipElements) {
var keyCode = e.keyCode || e.htmlEvent.keyCode;
if (keyCode === 13) {
var tabIndex = s.tabIndex || s.inputElement.tabIndex;
if (numSkipElements == undefined) {
numSkipElements = 0;
}
var nextElement = FindNextElementByTabIndex(tabIndex + numSkipElements);
if (nextElement != undefined) {
nextElement.focus();
return e.preventDefault ? e.preventDefault() : e.htmlEvent.preventDefault(); // this is the solution
}
}
}
functionFindNextElementByTabIndex(currentTabIndex, maxTabIndex) {
if (maxTabIndex == undefined) {
maxTabIndex = 100;
}
var tempIndex = currentTabIndex + 1;
while (!$('[tabindex='+ tempIndex+ ']')[0] || tempIndex === maxTabIndex) {
tempIndex++;
}
return $('[tabindex=' + tempIndex + ']')[0];
}
Post a Comment for "Focus Button From Javascript Withour Clicking It"