简体   繁体   中英

I need to give an alert when the user places the cursor in a textbox in asp.net. How do I go about doing this?

I need to give a custom alert to the user when the user places the cursor in a textbox item in asp.net. How do I go about doing this? Please help.

<input type="text" onfocus="alert('Got focus!');"/>

or a bit more involved:

<script>
  function InputFocus()
  {
    var inp = document.getElementById('myInput');
    inp.onfocus = null; 
    alert('Got focus - ' + inp.id);
    setTimeout(function() { inp.onfocus = InputFocus; }, 100);
  }
</script>

<input type="text" value="one"/>
<input id="myInput" type="text" onfocus="InputFocus();" value="two"/>
<input type="text" value="three"/>

Javascript on focus event.

On Page_Load or Page_Init method add this code:

 mytextBox.Attributes.Add("onfocus", "enterTextBox();")

Then on the page add a script tag with this :

function enterTextBox() {
     alert('hello');
}

the two events that you need are onfocus (elemant has focus and can accept input) and onblur which gets fired when leaving the element (say a text box). Disabled elements cannot have focus so these events will not occur in that case.

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