Stupid onClick="select()"-question

Discussion in 'JavaScript' started by PoPSiCLe, Mar 14, 2009.

  1. #1
    I have a form field (okay, more than one) where I would like to have the text selected when I focus/click on the text.

    I can't get any of the online scripts to work, though - I've tested many different ones, ranging from the simplest "select.this" onClick-event to more elaborate scripts.

    The only thing that seems different from when I test the scripts online, to my own pages, is the DOCTYPE - I'm running:
    
    <?xml version="1.0" encoding="utf-8" ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml11.dtd">
    
    Code (markup):
    and I know that this sometimes can mess with the javascript execution.

    So, does anyone have any idea as to how I can get a simple "select_all_text_in_form_field" to work with the above mentioned DOCTYPE?

    The form itself can be seen here: http://www.regncon.no - it's the login-field in the upper-right corner.
     
    PoPSiCLe, Mar 14, 2009 IP
  2. Dondon2d

    Dondon2d Peon

    Messages:
    3,193
    Likes Received:
    146
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try onFocus="this.select()".
     
    Dondon2d, Mar 14, 2009 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    Tried it - does not work. It's actually up on the page now - you can see for yourself it does not trigger anything when clicking the input field.
     
    PoPSiCLe, Mar 14, 2009 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Well well - I found something that worked, actually:
    
    onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"
    
    Code (markup):
    It doesn't actually select the text, but it replaces it with nothing, which is just as good. Works perfectly for what I need it for.
     
    PoPSiCLe, Mar 14, 2009 IP