SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Addict picasso-trigger's Avatar
    Join Date
    Dec 2003
    Location
    somewhere out there
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    why doesn't this simple code work?

    Dreamweaver has this builtin js script to wipe out the data in a field ... useful for erasing the "enter in your search term here" from input fields by simply clicking in there.

    But it has this annoying side effect that erases the value all the time. i jsut want it to wipe out the value on the first click ... as the user might enter in partial data, cope text somewhere else, then go to click the rest of the data in the field ... but this erases all the data again ... annoying.

    so i figure, i'll jsut set a variable after it runs the first time and at the beginning on the function check if the variable is set, if not ... erase the value, if it is ... don't erase it.

    so i'm a total JS newbie ... i code in php but new to JS ... why doesn't this work? it erases my data everytime i click into it ...

    Code:
    function MM_setTextOfTextfield(objName,x,newText) { //v3.0
    if (!(clickedalready == 1)) {
      var obj = MM_findObj(objName);
      if (obj) obj.value = newText;
      var clickedalready = 1;
    }
    }
    _________________________

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by picasso-trigger View Post
    Code:
    function MM_setTextOfTextfield(objName,x,newText) { //v3.0
    if (!(clickedalready == 1)) {
      var obj = MM_findObj(objName);
      if (obj) obj.value = newText;
      var clickedalready = 1;
    }
    }
    That is a local variable that is destroyed when the function terminates. Instead you could just insert this onfocus handler in the input/textarea tag:

    Code:
    onfocus="if(this.value==this.defaultValue)this.value=''"
    Also to restore the original text if nothing is entered:
    Code:
    onblur="if(this.value=='')this.value=this.defaultValue"
    Tab-indentation is a crime against humanity.

  3. #3
    SitePoint Addict picasso-trigger's Avatar
    Join Date
    Dec 2003
    Location
    somewhere out there
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ali ... nice, much better ... does exactly what i want and less code to! thanks!
    _________________________


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
  •