SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can this be made into one function?

    Hi all,

    I have the following JavaScript which is used in conjunction with the login form (also displayed) so that when a user focuses on one of the text boxes the value is cleared - but only if the default value is in there at the time. Get me?
    Code:
    function clearUN()
    {
    if(document.forms['loginform'].elements['username'].value == 'Username')
    {
    document.forms['loginform'].elements['username'].value = '';
    }
    }
    function clearPW()
    {
    if(document.forms['loginform'].elements['password'].value == 'Password')
    {
    document.forms['loginform'].elements['password'].value = '';
    }
    }
    function fillUN()
    {
    if(document.forms['loginform'].elements['username'].value == '')
    {
    document.forms['loginform'].elements['username'].value = 'Username';
    }
    }
    function fillPW()
    {
    if(document.forms['loginform'].elements['password'].value == '')
    {
    document.forms['loginform'].elements['password'].value = 'Password';
    }
    }
    // -->
    </script>
    And the login form:
    HTML Code:
    <form action="quelquechose.php" method="post" id="loginform">
    <input type="text" name="username" class="textbox" value="Username" size="15" onfocus="clearUN();" onblur="fillUN();" />
    <br />
    <input type="password" name="password" class="textbox" value="Password" size="15" onfocus="clearPW();" onblur="fillPW();" />
    <br /><br />
    <input type="submit" name="submit" class="button" value="Log in">
    </form>
    What I want to know is, how can I condense the above four functions into one single function? Is it even possible?

    Thanks in advance [img]images/smilies/smile.gif[/img]

    -Sam
    Sam Hastings

  2. #2
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Do you mean like this?


    function resetFld(isOk, fieldObj)
    {
    fieldObj.value = (isOk) ? ( (fieldObj.type == 'password') ? "Password" : "UserName" ) : "";
    }

    <input type="text"...onFocus="resetFld(0, this);" onblur="resetFld(1, this);" />
    <input type="password"...onfocus="resetFld(0,this);" onblur="resetFld(1,this);" />

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •