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?

StackOverflow https://stackoverflow.com/questions/13651210

Question

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.

Was it helpful?

Solution 2

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');
}

OTHER TIPS

<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"/>

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top