?
Solved

javascript on focus auto text

Posted on 2011-09-14
2
Medium Priority
?
357 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
[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
2 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 36539969
>>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:
Gurvinder Pal Singh earned 2000 total points
ID: 36540905
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…
Suggested Courses

800 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