How to set focus to table using javascript
Focus_table.aspx
Get Focus on Table
window.onload = function() {
document.getElementById('tblFocus').focus();
};
You have to give the id to the table. When you will run this code snippet then focus will be set to the desired table but this code snippet will not work in firefox.
Now to solve the browser compatability issue you have to put any form field in the table that you want to focus and then using javascript code you have to focus that form field and due to that, focus will be automatically set to that table. So let's have a look over this solution
window.onload = function() {
document.getElementById("<%=txtFocus.ClientID %>").focus();
};
Get Focus on Table
Now this code will run on almost all browsers but there is a drawback with it. The cursor will directly blink in the textbox and due to this you cannot go to previous page with the Backspace button of your keyboard. Also it is very awkward that cursor is blinking in textbox. But if you hide the textbox using css i.e; style="display:none;" and call the javascript function to focus the textbox then javascript function will throw an error.
Now to get rid of this issue, there is a solution and that solution is to use tag. Let's have a look over it.
window.onload = function() {
document.getElementById('aFocus').focus();
};
Get Focus on Table
window.onload = function() {
document.getElementById('tblFocus').scrollIntoView(true);
};
0 comments:
Post a Comment