html setfocus on textarea


Hi Expert,

I have a survey.aspx.vb page

strLabel = strLabel.Trim & "<textarea rows=""7"" cols=""100"" style=""" & strStyle.Trim & """ name=""Comment" & intQuestionNo.ToString.Trim & """>" & strCurrComment.Trim & "</textarea></td></tr>"


lblQuestions.Text = strLabel

If user not put anyting in textarea, I want set focus on this textarea, how I can do it?

Thanks in adance.
LVL 1
dshi15Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

khan_webguruCommented:
Try this example

 
<html>
    <head>
    <script language="JavaScript">
    <!--
    function setFocus(num){
      if(num == 1){
        document.myForm.myTextArea1.focus();
      }else if(num == 2){
        document.myForm.myTextArea2.focus();
      }
    }
    -->
    </script>
    </head>
    <body>
    <P>
    <form name="myForm">
      <textarea name="myTextArea1" rows=2 cols=50>
      Here is the first text area.
      </textarea>
    <input type=BUTTON value="Click to Set Cursor" name="myButton1" onClick="setFocus(1)">
      <br>
      <textarea name="myTextArea2" rows=2 cols=50>
      Here is the second text area.
      </textarea>
      <input type=BUTTON value="Click to Set Cursor" name="myButton2"
             onClick="setFocus(2)">
    </form>
    </body>
    </html>

Open in new window


Live demo

http://www.java2s.com/Tutorial/JavaScript/0200__Form/Textareafocus.htm
0
khan_webguruCommented:
0
dshi15Author Commented:
I want add some code in my exiting code, I just don't know how to add

My code like that

If strCurrComment ="" then

strLabel = strLabel.Trim & "<textarea  setfocus=true rows=""7"" cols=""100"" style=""" & strStyle.Trim & """ name=""Comment" & intQuestionNo.ToString.Trim & """>" & strCurrComment.Trim & "</textarea></td></tr>"

Else

strLabel = strLabel.Trim & "<textarea rows=""7"" cols=""100"" style=""" & strStyle.Trim & """ name=""Comment" & intQuestionNo.ToString.Trim & """>" & strCurrComment.Trim & "</textarea></td></tr>"


End if


How to replace  "setfocus=true" and let it happen?


I had asp button Submit on  this page.

0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

khan_webguruCommented:
When you render this on page means when it will become part of the page right after that call like this

Textbox1.Focus()

but control should be run at server. Why you are not adding dynamic control because after that you can easily call this
0
khan_webguruCommented:
like this

 
Dim txtBox As New TextBox
txtBox.ID = "txtArea"
txtBox.TextMode = TextBoxMode.MultiLine
txtBox.Columns = 10
form1.Controls.Add(txtBox)

Open in new window


after the last line from above call this

txtArea.focus()

where txtArea is ID of the control that we are adding
0
dshi15Author Commented:
I worked on already exiting code, I don't want make big change on somebody else code.
0
khan_webguruCommented:
@dshi15: please sent me next few line of code also where this string is going to render on HTML. because after when this textarea will be part of the HTML we can apply the solution then hope you are getting my point
0
khan_webguruCommented:
I implemented following application here at my machine for you that is running fine.

Add this in <Head> tag of your application

 
<script language="javascript" type="text/javascript">
        function setCursor() {
            document.getElementById('myTextArea').focus();
        }
</script>

Open in new window


Where "myTextArea" would be textarea ID

If you already have some javascript funcation added in the head section then simply add the following function in <script> tag

 
function setCursor() {
            document.getElementById('myTextArea').focus();
        }

Open in new window


then I put an asp.net button on page and on click event I called a server side event and add a textarea from code behind into the page

like this

 
lblMsg.Text = "<textarea id='myTextArea'  setfocus=true rows='7' cols='100' name='Comment'> Hello </textarea>";

Open in new window


here lblMsg is my already added .net control in which I am adding textarea.

when it become part of the page then I am calling javascript function for cursor setting from code behind like this

 
string jScript = "<script>setTimeout('setCursor()',2000)</script>";

ClientScript.RegisterClientScriptBlock(this.GetType(), "jScript", jScript);

Open in new window




This will wait just for 2 second so that page load successfully to call client side method and can get textarea that we added.

Hope that will help you.

Regards,

AAK

0
khan_webguruCommented:
As you are looking for VB .Net then you can use like this little change according to your code

Add this in head TAG

<script language="javascript" type="text/javascript">
        function setCursor(textAreaId) {
            document.getElementById(textAreaId).focus();
        }
</script>

Open in new window


And where you have these line of code

 
Label = strLabel.Trim & "<textarea  setfocus=true rows=""7"" cols=""100"" style=""" & strStyle.Trim & """ name=""Comment" & intQuestionNo.ToString.Trim & """>" & strCurrComment.Trim & "</textarea></td></tr>"

Else

strLabel = strLabel.Trim & "<textarea rows=""7"" cols=""100"" style=""" & strStyle.Trim & """ name=""Comment" & intQuestionNo.ToString.Trim & """>" & strCurrComment.Trim & "</textarea></td></tr>"


End if

Open in new window


Right after this add these lines

 
Dim strScript As String = "<script>setTimeout('setCursor('Comment"&intQuestionNo.ToString.Trim&"')',2500)</script>"
Page.RegisterStartupScript(this.GetType(),strScript)

Open in new window


That all and here we go just test this :)
0
khan_webguruCommented:
Try my last post if and only if you will face some error then just replace the last part of code by this

 
Dim strScript As String = "<script>setTimeout('setCursor('Comment"&intQuestionNo.ToString.Trim&"')',2500)</script>"
Page.RegisterStartupScript("callTest",strScript)

Open in new window


Thats all from my side

Best of luck for your project

Regards,

AAK
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dshi15Author Commented:
Thank you very much and  I will try it and let you know tomorrow.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.