We help IT Professionals succeed at work.

Javascript change input value

Jamel_Prog
Jamel_Prog asked
on
414 Views
Last Modified: 2012-06-21
Hi Experts,

I am looking for a very simple code that can has a value on sign in inputs, so it will has username, but when click it will hide the username text so the user can enter his username. In case the user didn't write anything in the input and go out of the input, then username text should return.

I am using this:
<input type="text" value="username">
But by using this it will not be hidden when the user click on it, or return if the user didn't write any thing and take the selection out of the input.

Looking forward for your help, and thanks in advance

Regards
Jamel
Comment
Watch Question

CERTIFIED EXPERT

Commented:
Instead of type="text" use type="hidden"

Author

Commented:
Hi, thanks for comment, but this is not my question, an example of my question is that:
https://animoto.com/sign_in?return_to=%2Freferrals
see the email input, it has e.g. me@animoto.com
and when you click on it, it will hide, and when not complete anything and go out of the input then it will return the same text:)
Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Top Expert 2013

Commented:
Try:
<input type="text" value="username" onfocus="this.value='';"
   onblur="if (this.value=='') {this.value='username';}">


Cd&

Author

Commented:
Sorry COBOLdinosaur, I didn't see your comment when accepts the answer, thanks for your help really appreciate it :)
Kyle HamiltonData Scientist
Most Valuable Expert 2014

Commented:
<input type="text" id="username" value="username" />

<script>
$(function(){
var user=$("#username");

user.focus(function(){

if(user.val() == "username"){
user.val("");
}).blur(function(){
if(user.val() == ""){
user.val("username");
}
});
});
</script>

that's using jquery, so remember to include it.
Kyle HamiltonData Scientist
Most Valuable Expert 2014

Commented:
Haha, y'all beat me to it as I was typing!

:)))

Author

Commented:
@kozaiwaniec, I am happy for your help, and sorry for not seeing it before, but I will post lots of questions these days as I am beginner in Javascript so will be happy to get your help, thanks :)
Top Expert 2013

Commented:
@Jamel_Prog,

There is no problem. Leakim answered pretty completely and first. You don't have to appologize for getting an answer quickly.  I wish all the question could be dealt with this fast.


Cd&

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.