Link to home
Start Free TrialLog in
Avatar of astro26
astro26

asked on

onFocus highlights all data in textbox

I have a form which users must enter currency.  I have default values of 0.00, how can I highlight the entire '0.00' so that if a user enters 7, the value in the textbox is 7 and not 70.00 (7 prepended to 0.00)

<input type="text" name="total" value='0.00' /><br />
<input type="text" name="total2" value='10.00' /><br />
<input type="text" name="total3" value='5.00' /><br />
<input type="text" name="total4" value='12.00' /><br />

Question
When the user enters the textbox (if enabled) via mouse or tab keypress, how can I let the user just enter '7', instead of deleting "0.00", then entering "7".  

in otherwords, can I highlight the value in the textbox in an "onfocus???" or similar event.

TIA
Avatar of havik83rs
havik83rs

You could use this:

  // This function clears text away when the textfield is focused
  function clearText(field) {

    if(field.defaultValue == field.value)

      field.value = ""

  }

<input type="text" name="total" value='0.00' onfocus="clearText(this)"/><br />

It clears the text automatically for the user.

Havik
Avatar of fritz_the_blank
<INPUT type="text" id=text1 name=text1 onFocus="(this.select())" >


Fritz the Blank
Avatar of astro26

ASKER

Thanks for the responses!
havik83rs was close, but I do not want to clear the box, incase the user is just tabbing through the form and making no changes.
Fritz, this works great for tab.  
If I was using a mouse, would I use onmouseup?  if so, how would the user select 1 digit they wanted to change.  are the mouse events good to use?

TIA
ASKER CERTIFIED SOLUTION
Avatar of fritz_the_blank
fritz_the_blank
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of astro26

ASKER

Great Answer, the mouse event didn't fire for me at first, but I copied it exactly as above and it works.

thanks for bearing with me, and thanks for the lightning fast response.
Glad to have helped,

Fritz the Blank