SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Disable the function itself after first click?

    Is it possible for javascript to disable it's own function after the first click?

    I have this function to clear an input field:

    Code:
    function clearTextBox(Object, formValue) {
    	if(Object.value == formValue) {
    		Object.value = "";
    	}
    }
    When I call it in my form, I have this:

    Code:
    clearTextBox(this, this.value);
    Of course, the problem is that every time a user clicks on the text field, it's going to clear whatever is there, even if they already entered something of their own. I would rather for it to clear only the first time (for example it will by default say "Your Name Here").

    What I'm wondering is if it would be possible to have the function disable itself somehow after the first time it's called. Any suggestions?

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Depends on how the function is called.
    If you use field.onfocus=clearTextBox;
    add the line Object.onfocus='' after you empty the text box.
    Or you could see if Object.value=='Your name here" before clearing it.

    It is better to put the field's description in a label next to the input than in the input itself.
    Chances are a user won't remember what goes where, especially if he uses tab to move through the form.

  3. #3
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks mrhoo - I would rather not check if Object.value=='Your name here" however... as I would like to find a way to use the same function again (for multiple input fields).

    The problem is that when I check if Object.value == this.value it gets interpreted as if Object.value == "whatever the value is at the time the user clicks". And that clears anything that is there on click. I suppose I could just set a value = 1, increment it, then check what the value is... but I'm sure there's a better way!

  4. #4
    SitePoint Zealot
    Join Date
    Apr 2008
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why not "cache" in a variable the default value of the input?

    let's say, if your textbox has an initial value of "click here", in your function you can set a variable called initial = textbox.value;
    and when the user changes the text inside that input you can check if the current value matches the initial value and return false (do nothing)...
    Proud member of this community.
    ____________________________

  5. #5
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could also set a title equal to the temporary value.
    That way a mouseover will show the 'Your name ' or whatever, and the input will only clear if it is the same as the title.
    if(Obj.value == Obj.title)

    // It is a bad idea to have a variable called Object!


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
  •