1

I really don't understand why this is being so hard for me to get working:

It will eventually be going into an ASP.NET control, so I believe I need to pass Javascript the <textarea> control's ID.

It doesn't work.

So, in the jsFiddle, I tried using standard controls for First and Last names, but they don't work either.

EDIT:

I've been playing around with the jsFiddle, and it appears that my jsOnFocus is never called (alert never fires). However, I was able to run down a little script from someone else that makes the multiline textbox clear and reset - I just can't seem to find a way to call this from a javascript function:

  function jsOnFocus(obj) {
      alert("Inside the jsOnFocus.");
      if (obj.Value==obj.defaultValue)
          obj.Value="";
  }
  function jsOnBlur(obj) {
      if (obj.Value==="")
          obj.Value=obj.defaultValue;
  }

​ Here is the HTML.

  <table>
    <tr><td>Message:</td><td>&nbsp;</td></tr>
    <tr>
      <td>&nbsp;</td>
      <td>
        <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>
      </td>
    </tr>
    <tr>
      <td>Test1:</td>
        <td><input type="text" name="txtTest1" 
          onfocus="jsOnFocus(txtTest1)" 
          onblur="jsOnBlur(txtTest1)" value="Test1" /></td>
    </tr>
    <tr>
      <td>Test2:</td>
  <td><input type="text" name="txtTest2" 
          onfocus="if(this.value=='Test2'){this.value=''};" value="Test2" /></td>
    </tr>
    <tr>
      <td>Test3:</td>
        <td><input type="text" name="txtTest3" 
          onfocus="if(this.value==this.defaultValue)this.value='';" 
          onblur="if(this.value==='')this.value=this.defaultValue;" value="Test3" /></td>
    </tr>
  </table>​

"Message" works, but I'd like to get the code to running in a javascript that I can place in my "js" file and use on other objects.

"Test1" is an attempt to call the javascript, but it does not work.

"Test2" works, but it does not use the technique in "Message".

"Test3" works, and it does use the technique in "Message".

Does anyone see how to make this code work in the javascript instead of inline html?

jsFiddle Link: http://jsfiddle.net/jp2code/qCExy/10/

  • 1
    There are some errors JSLint picks up in your jsFiddle. But even when I fixed those and added an alert to jsFocus1 it was not triggered onfocus, though adding the same alert to the onfocus call directly did trigger the alert. So I'm not sure what's going on there – Simon Martin Aug 14 '12 at 21:56
  • Did you save that jsFiddle? I'm interested in learning what you did to correct the errors that JSLint picked up. I'm not great at Javascript. If you post it as an answer, I certainly won't downvote you. –  Aug 15 '12 at 12:56
  • 1
    I have just updated the fiddle with the changes I made to get JSLint happy; == should be === and some double quotes became single quotes because they were inside a string. – Simon Martin Aug 15 '12 at 13:00
  • I got part of it working in http://jsfiddle.net/jp2code/qCExy/10/, and I posted up a more helpful question with updates. –  Aug 15 '12 at 16:16
  • 1
    I am not sure if this will be of any help but I was trying to do the same thing on the username/password field using javascript & then ended up using a placeholder (easy fix but wont work in some IE v) :) http://jsfiddle.net/qCExy/11/ just take a look it won't harm!!! – Scorpio Aug 15 '12 at 17:11
  • `Placeholder` is *NICE*! I found myself looking all over trying to find how you'd done the CSS, then noticed there was no javascript, then... ah! :) –  Aug 15 '12 at 17:31
  • yep no JS, no CSS, clean one line code or maybe just 2 words :) but as I said its not yet implemented in all the browsers specially IE... – Scorpio Aug 15 '12 at 17:35
  • if you want to stick to JS then you will have to do some modification on your Code Behind,hmmmm... its exceeding the char limit I will add it as answer.... – Scorpio Aug 15 '12 at 17:39
  • 1
    Here it is working in JS with no inline HTML... http://jsfiddle.net/qCExy/13/ – skyline3000 Jan 04 '13 at 23:05

2 Answers2

1

Code Behind:

            string strUserName = "User Name";
            string strPassword = "Password";

            txtUserName.Text = strUserName;
            txtPassword.Text = strPassword;

            txtPassword.Attributes.Add("onblur", "PasswordBlur(this, '" + strPassword + "');");
            txtUserName.Attributes.Add("onblur", "UserNameBlur(this, '" + strUserName + "');");

            txtUserName.Attributes.Add("onfocus", "UserNameFocus(this, '" + strUserName + "');");
            txtPassword.Attributes.Add("onfocus", "PasswordFocus(this, '" + strPassword + "');");

Java Script:

function UserNameBlur(txtElem, strUserName) {
    if (txtElem.value == '') txtElem.value = strUserName;
}
function PasswordBlur(txtElem, strPassword) {
    if (txtElem.value == '') txtElem.value = strPassword;
}
function UserNameFocus(txtElem, strUserName) {
    if (txtElem.value == strUserName) txtElem.value = '';
}
function PasswordFocus(txtElem, strPassword) {
    if (txtElem.value == strPassword) txtElem.value = '';
}
Scorpio
  • 1,151
  • 1
  • 19
  • 37
  • I started out doing this in code behind, but slowly worked forward to what I posted in my original question when nothing was working. I'll see about adding your version to my project when I get back to it. This may (i.e. may not) do it. –  Aug 15 '12 at 18:06
1

I played around a bit with your fiddle. I'm a newb when it comes to jsfiddle though so I did not dare to try to save my changes.

Anyway, there are a few things that keep the Test1 box from working. First of all, the script in the "JavaScript" block in the fiddle is added to the page like this (check by viewing source in the Result section):

<script type='text/javascript'>//<![CDATA[ 
    window.addEvent('load', function() {
        function jsOnFocus(obj) {
            alert("Inside the jsOnFocus.");
            if (obj.Value==obj.defaultValue)
                obj.Value="";
        }
        function jsOnBlur(obj) {
            if (obj.Value==="")
                obj.Value=obj.defaultValue;
        }
    });//]]>  
</script>

So your functions are not in a scope (for lack of a more precise term) where you can call them like you tried to do.

To fix this I added the script directly in the Html section inside as <script> block.

There was also a small issue with the javascript code itself. The value property is called value, not Value.

Also, I changed the onfocus and onblur attributes on the input to pass this to the functions. You can of course change it to pass an id if you want and then look up the control using the id. In that case make sure that the control has an id specified.

Anyway, here is the resulting code from the Html section of the fiddle:

<script type="text/jscript">
    function jsOnFocus(obj) {
        if (obj.value==obj.defaultValue)
            obj.value="";
    }
    function jsOnBlur(obj) {
        if (obj.value==="")
            obj.value=obj.defaultValue;
    }
</script>
<table>
  <tr><td>Message:</td><td>&nbsp;</td></tr>
  <tr>
    <td>&nbsp;</td>
    <td>
      <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>
    </td>
  </tr>
  <tr>
    <td>Test1:</td>
      <td><input type="text" name="txtTest1" onfocus="jsOnFocus(this)" onblur="jsOnBlur(this)" value="Test1" /></td>
  </tr>
  <tr>
    <td>Test2:</td>
      <td><input type="text" name="txtTest2" onfocus="if(this.value=='Test2'){this.value=''};" value="Test2" /></td>
  </tr>
  <tr>
    <td>Test3:</td>
      <td><input type="text" name="txtTest3" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value==='')this.value=this.defaultValue;" value="Test3" /></td>
  </tr>
</table>
user1429080
  • 9,086
  • 4
  • 31
  • 54
  • I'm looking at this now, but wanted to comment on that jsfiddle site: Anything you do can be saved, because it keeps some sort of version control in the hyperlink. So, when you save a version, it increments the URL to the next available version number, and that URL is something you are allowed to post. Pretty cool tool! –  Jan 04 '13 at 15:22
  • God, it's working, though {shock}. So, why does one line of the javascript code I found use three (3) equal signs? `if (obj.value==="")` –  Jan 04 '13 at 16:44
  • See here http://stackoverflow.com/questions/359494/javascript-vs-does-it-matter-which-equal-operator-i-use – user1429080 Jan 05 '13 at 06:54
  • Good enough. Thanks for all your help on this. –  Jan 05 '13 at 14:14