SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict Percipient's Avatar
    Join Date
    Jun 2000
    Location
    Summerland, BC, Canada.
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have a page with multiple text fields, each of which has an example for an initial value. I've added the appropriate code so that onFocus() the example is cleared away, and a blank field is left behind. Trouble is, I only want that to happen once, so that the field isn't cleared if the user attempts to edit what they wrote earlier. Anybody know of a way to do that with only a few lines of code? Here's my existing code:

    Code:
    <script language="JavaScript">
    <!--
    function clearField(fieldName) {
     fieldName.value = "";
    }
    // -->
    </script>
    
    <p>Person's Name: <input type="text" name="name" size="30"
    value="John Smith" tabindex="2" onFocus="clearField(this)"></p>
    Ed Rands

  2. #2
    SitePoint Member
    Join Date
    Apr 2001
    Location
    eastcoast
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this:
    <script>
    function clearIt(){
    var form1 = document.form1
    if (form1.test.value == "type sompthing")
    form1.test.value = ""
    }
    </script>

    <input type="input" name="test" value="type sompthing" onClick="clearIt()">
    Flash kit Moderator

  3. #3
    SitePoint Member Slash's Avatar
    Join Date
    Sep 2000
    Location
    Egypt
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hereis a more generic solution for your porblem, if u still searching for an answer .

    <script language="JavaScript">
    <!--
    var x = -1; // used as loop counter.
    var ExClearedInputs = new Array(); //Array that holds your previously cleared input fields names.

    function clearField(fieldName) {
    var i;
    var ClearedBefore = false;

    //searching the past cleared input field names array.
    for (i=0; i<=x; i++)
    {
    if (ExClearedInputs[i] == fieldName.name) {ClearedBefore = true;}
    }

    if (ClearedBefore != true)
    {
    x++;
    ExClearedInputs[x] = fieldName.name;
    fieldName.value = "";
    }
    }
    // -->
    </script>


    This will work with any input field, only when you call the function from its eventHandler.
    The function will hold the Ex-Cleared input field names in an Array. And when called again, it will check check its existance in the array first, then perform the right action toward this input. Voila!


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
  •