0

I've got a multiline asp.textbox input control.

I don't know if my issue is with ASP.NET, the Multiline control, or something else, but the onblur and onfocus are not working.

<script type="text/javascript">
  var defaultMsg = 'Write your message here or or call my voice at (xxx) xxx-xxxx.';
  var controlMsg = doc.createElement("input");
  controlMsg.id = "txtMessage";
  controlMsg.type = "text";
  controlMsg.value = defaultMsg;
  controlMsg.onfocus=function jpFocus() { if (this.value == defaultMsg) this.value = ''; }
  controlMsg.onblur=function jpBlur() { if (this.value == '') this.value = defaultMsg; }
</script>

And later....

<asp:TextBox ID="txtMessage" Columns="30" Rows="6" runat="Server" TextMode="MultiLine" />

Does anyone see a reason why this should not be working?

Pravin Pawar
  • 2,559
  • 3
  • 34
  • 40

4 Answers4

1

Actually, you're creating a html element and you are attaching an event to it. In addition, ASP.NET controls does not use the server side id. Here's what you should do :

var controlMsg = document.getElementById('<%= txtMessage.ClientID %>');
controlMsg.onfocus = // Some Code ...
controlMsg.onblur = // Some Code ...
Haythem Tlili
  • 566
  • 3
  • 10
  • That's still not working. I've tried several variations of this, and my page isn't happy with anything I do. I just noticed I changed my text, but the old text is still showing. Perhaps my localhost is caching the data and not showing my latest changes. –  Jul 31 '12 at 01:45
1

Try using an anonymous function, like this:

controlMsg.onfocus=function() { if ( ...

Functions and function scope - MDN.

Also, you did call something like document.body.appendChild(controlMsg);, didn't you?

EDIT:

You are using doc.createElement. Make sure that doc definitely points to document.

Also, look at the page in Firefox and see if there are any errors or warnings in the Error Console.

uınbɐɥs
  • 7,236
  • 5
  • 26
  • 42
  • You may be onto something. I've got some sort of [Client Side validation script being dumped to the screen](http://stackoverflow.com/questions/11751725). Would you happen to understand if functions and scope could be causing this? –  Aug 02 '12 at 15:55
0

Not sure which version your are using.Please Try this:

protected void Page_Load(object sender, EventArgs e)  
 {
   txtMessage. Attributes["onfocus"]="JavascriptMethod";  
 }
Nag
  • 689
  • 1
  • 5
  • 15
0

I'm not sure why this worked and other techniques did not, but this got my HTML to work:

    <textarea name="txtMsg" rows="6" cols="30"
      onfocus="if(this.value==this.defaultValue)this.value='';" 
      onblur="if(this.value==='')this.value=this.defaultValue;">[Write your message here or call my voice number at (555) 222-1234.]</textarea>

The text can be very long, and the control does not seem to care. Also, the text is only written in a single location.