Results 1 to 8 of 8

Thread: text appearing in a textbox

  1. #1
    Join Date
    Aug 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default text appearing in a textbox

    Hi there - I have a simple query:

    I am looking for some javascript code for an email text box:.

    When the page loads the text in the text box will state
    "Enter email address"

    When the user clicks in the text box - the text will automatically disappear allowing the user to write his/her email address.

    Thank you


  2. #2
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    It's really easy to make something like that.

    here's the javascript:
    Code:
    <script type="text/javascript">
    function erase(){
    var target = document.someform.somename;
    target.value = "";
    }
    </script>
    here the form:
    Code:
    <form name="someform">
    <input type="text" name="somename" onfocus="erase();" value="Enter your email address here">
    </form>
    there is also a more easier way:

    Code:
    <form name="someform">
    <input type="text" name="somename" onfocus="this.value=''" value="Enter your email address here">
    </form>

  3. #3
    Join Date
    Aug 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you - works great!

  4. #4
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    You are welcome.

  5. #5
    Join Date
    Aug 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    One extra question:

    Is it possible to only have that javacode run once (clearing the texdt box)

    Otherwise if the user has entered the wrong address then he/she when going back to edit the code - the text box blanks everytime.

  6. #6
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I didn't get what you meant but I have a strong feeling that you meant something like this:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function show(){
    var target = document.someform.someinput;
    if(target.value == ""){
    target.value = "enter your email";
    }
    }
    </script>
    </head>
    <body>
    <form name="someform">
    <input type="text" onfocus="this.value=''" onblur="show();" name="someinput" value="enter your email">
    </form>
    </body>
    </html>
    And it's javascript not java, java and javascript are *NOT* the same.

  7. #7
    Join Date
    Aug 2006
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Or This Might Work As Long As You Have The Rest Done..

    <body>
    <form name="someform">
    <input type="text" onFocus="this.value=' ' " onblur="this.value='Enter Your Email' name="someinput"
    </form>
    </body>

    Sorry If This Doesnt Help You

  8. #8
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    What the OP is really looking for is something like:

    Code:
    function clearDefaultValue(element) {
        if (element.value == element.defaultValue) element.value = '';
    }
    HTML Code:
    <input type="text" name="e-mail" value="Enter e-mail address"
        onfocus="clearDefaultValue(this);">
    That is, only clear the value of the element if it equals the default entry.

    Note that this isn't typically a good idea. It should be obvious through other means - a label, more often than not - what should be used as the value of a control. Fiddling like this is unnecessary, and might even be confusing to some.

    Mike

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
  •