How can I set focus on asp:TextBox which is placed inside a user control by using jquery/JavaScript function in aspx page?
Asked
Active
Viewed 2.9k times
3 Answers
9
You can use id selector and use focus() method. For using jQuery you need to include jQuery and ensure your element is available in DOM
before you access it. You can use document.ready for that.
$('#txt1').focus();
Or using plain JavaScript, focus()
document.getElementById('txt1').focus();
If you do not have ClientIDMode = static then you will need to use ClientID
$('#<%= txt1.Client %>').focus();
-
You might need to use ClientID, make sure you replace txt1 with your control id. – Adil Aug 19 '13 at 10:02
-
Adil i also tried "$('#<%= txt1.Client %>').focus();" as u said in the above response but its giving same error,according to me its not able to find textbox with the given id and thats why its giving that error. – Sid M Aug 19 '13 at 10:36
1
You can try with jQuery :
$('#txt').focus();

Pouki
- 1,654
- 12
- 18
-
You have to replace "#txt" with the real ID of the control to focus. ie: `$('#<%= txt.ClientID %>').focus();` if your asp control has the name `txt` – Pouki Aug 19 '13 at 11:40
-
yeah i replaced "#txt" with my textbox id and also tried "$('#<%= txt.ClientID %>').focus();", however the error still persists. – Sid M Aug 19 '13 at 11:53
-