Solved

javascript on focus auto text

Posted on 2011-09-14
2
351 Views
Last Modified: 2012-05-12
I have the following java-script codes and it is working fine.

When it load, it will show text in the textbox.
When it focus, it will show nothing in the textbox.
However, when it is not focus, i still want to see the text in the textbox.
Is it possible?

function clearDefault(el) {
    if (el.defaultValue == el.value) el.value = ""
}

<input class="search_box_bg" type="text"
name="TxtOriginCity" id="TxtOriginCity"
ONFOCUS="clearDefault(this)" value="Enter an origin city name">
0
Comment
Question by:Webboy2008
2 Comments
 
LVL 82

Expert Comment

by:hielo
Comment Utility
>>Is it possible?
For 500pts, the correct answer is: Yes!

:)

<script type="text/javascript">

function clearDefault(el) {
    if (el.defaultValue == el.value) el.value = "";
	else if(el.value=='')el.value=el.defaultValue;
}
</script>

<input class="search_box_bg" type="text" 
name="TxtOriginCity" id="TxtOriginCity" 
ONFOCUS="clearDefault(this)" value="Enter an origin city name"
ONBLUR="clearDefault(this)">

Open in new window

0
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
Comment Utility
why don't you use the placeholder attribute of html5?
http://www.w3schools.com/html5/att_input_placeholder.asp

which browser are you working on?
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now