简体   繁体   中英

how to set focus for label when click the any button using javascript

I have one asp:Textbox and one asp:label control in my aspx page. I want to set focus for label when clicking the button using javascript. Can anyone tell me how to do this?

Many thanks...

You need to access the element via an id or a class an call focus() :

document.getElementById('myTextBox').focus();    

As Pointy pointed out: When you mean by clicking on a label to select the textbox, you just need to set the for attribute on the label to the ID of the textbox.

<label for="myTextBox">Label Text</label>

The for attribute does it for you out of the box

<label for="{the id you want}"> </label>

MDN

Because it's asp.net application you need to use the ClientID :

<label for='<%= myTextBox.ClientID %>'> </label>
use `.focus()` it will do the focus part

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM