?
Solved

onFocus highlights all data in textbox

Posted on 2003-03-26
6
Medium Priority
?
748 Views
Last Modified: 2008-03-06
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
0
Comment
Question by:astro26
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 

Expert Comment

by:havik83rs
ID: 8213322
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
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8213465
<INPUT type="text" id=text1 name=text1 onFocus="(this.select())" >


Fritz the Blank
0
 

Author Comment

by:astro26
ID: 8213653
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
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 46

Accepted Solution

by:
fritz_the_blank earned 200 total points
ID: 8213820
Wel, even if they click on the field with a mouse, it still fires this code. Try it.

Fritz the Blank
0
 

Author Comment

by:astro26
ID: 8213982
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.
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8214045
Glad to have helped,

Fritz the Blank
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question