Forms: How to clear default text of input box upon selection - JavaScript
Have you seen how sometimes text boxes will have some default text in them that when you click on them goes away. Would you like that for your site, but don't know how? This brief tutorial will give you the details you need to get it done. It is a fairly simple piece of JavaScript to do it.
For an example of what we are trying to do:
First, I'll show you the code, then if you are interested, you can read on to get an explanation of what it all means.
Put this in the head section of your page (it can go in the page itself, but the head is a better place:
<script type="text/javascript">
function clearMe(formfield){
if (formfield.defaultValue==formfield.value)
formfield.value = ""
}
</script>
And this is the input box that goes into you form:
<input type="text" name="searchbox" value="Keyword Search" onfocus="clearMe(this)" />
With our input box, we need a name to reference it by. In this case, it is called searchbox. The value tells us what the default value will be. Finally, onfocus tells us that when this box is made active, by clicking on it or tabbing to it, or some other means, then the function clearMe is called and sent the value of this. this is a special JavaScript property that sends a reference to the html property it is laid in. In other words, it means "This HTML piece".
The script is really simple, just an if statement. You could just include it directly in the onfocus, but that does not lend to reusability, so it is best to stick a function in the head. We will dissect it line by line.
<script type="text/javascript"> - Tells the browser that the following script is in JavaScript. It ends with </script>. So everything between the two must be in JavaScript.
function clearMe(formfield){ - This starts the function. The name of the function is give and then in parenthesis are any variables that are passed to the function. In this case the function is named "clearMe" and it is passed one variable called "formfield".
if (formfield.defaultValue==formfield.value) - Is a simple if statement. This simple says that if the form field (passed from the input box) has the same default value and current value then we proceed. If not, we skip the next line of code. Since there is no { after the statement, only one line will be used. If you wanted to have multiple lines used for the if, then you would enclose them in { and }, just like the function.
formfield.value = "" - If the if statement above was true then this line changes the current value of the form field to be blank.
} - ends the function. The function is surrounded in { and } so the browser knows where it begins and ends.
</script> - As I mentioned above, this tells the browser that we no longer are using a scripting language and to go back to HTML.
Next, I'll take this one step further and show you how to return the default value if they leave the form field blank.
- 16183 reads
by email 




Comments
DSFGD
1091014733PMwow gold | World of Warcraft gold | buy wow gold | cheap wow gold | Atlantica Online Gold | Atlantica Gold | 2moons gold | 2moons dil | Metin2 yang | Metin2 gold | Aion gold | aion Kinah | aion money | buy aion kinah | cheap aion gold | aion online gold | Aion ?????? | Aion ??????? | ???????? Aion ?????? | ??????? Aion? ??????? | buy Aion gold | cheap aion kina | Archlord Gold | Buy Archlord Gold | Flyff Penya | Flyff gold | Last Chaos Gold | Last Chaos Money | Knight Gold | Knight Noah | knight online gold | knight online noah | Silkroad Gold | Silkroad Gold | SRO Gold | SRO Gold | Silkroad online gold | Silkroad online gold | Silkroad Gold | Silkroad Gold | SRO Gold | SRO Gold | Silkroad online gold | Silkroad online gold | Hero Online Gold | Hero Gold | Rohan Gold | Rohan Crone | twelvesky2 gold | 12sky2 gold | 12Sky2 Silver | buy 12Sky2 Silver Coins | Dragonica gold | Dragonica Online gold | cheap Dragonica gold | buy Dragonica gold | Jade Dynasty gold | JD gold | buy Jade Dynasty gold | cheap jade gold | Perfect World Gold | Perfect World coins | Perfect World silver | RF gold | RF online gold | Warhammer Gold | Warhammer Online Gold | Runescape Gold | Runescape2 Gold | Shaiya Gold | Shaiya Money | ESO GOLD | ether saga online gold | The Chronicles of Spellborn Gold | TCOS Gold | Age of Conan Gold | AoC Gold | Lineage2 Gold | Lineage2 Adena | Fiesta Online Gold | Fiesta gold | ROMO Gold | buy ROMO GOLD | Requiem Gold | Requiem Lant | Rappelz Gold | Rappelz Rupees | 9Dragons Online Gold | 9Dragons Gold | Scions of Fate Gold | SOF Gold | Lotro Gold | The lord of rings gold | Karos Carat | karos gold | karos online gold | Dofus gold | dofus kamas | Cabal gold | Cabal alz | Cabal money | cheap Cabal Alz | wk zed | wonder king zed | wonderking gold | wk gold | wonderking online gold | Star Trek Online Credits | STO Gold | star trek gold | star trek online gold | 4 Story Gold | 4 Story Luna | allods gold | allods online gold | allods ol gold | buy allods gold | ?????? ?????? | ?????? ?????? ?????? | ?????? ?????? ?????? | ??????? ?????? | Maplestory | Maple Story Mesos | MapleStory Mesos | Maple Story Meso | DFO gold | DF online gold | Dungeon Fighter gold | Dungeon Fighter Online gold | Mabinogi Gold | Mabinogi money | buy Mabinogi Gold | cheap Mabinogi money | Dragon Oath Gold | Cheap Dragon Oath Gold | Dragon Oath money | Buy Dragon Oath money | Grand Fantasia Gold | buy Grand Fantasia Gold | GFO gold | Cheap Grand Fantasia Gold | aika gold | aika online gold | cheap aika gold | aika money | Lunia Online Gold | Lunia Gold | Lunia Soldin | Cheap Lunia Gold | Ragnarok Zeny | RO Zeny | RO Gold | Ragnarok Online Zeny | FFXIV Gil | FFXIV Gold | tera gold | tera online gold | buy tera gold | cheap tera gold | Boi Gold | Battle of immortals Gold | Buy Boi Gold | cheap battle of the immortals Gold | CrimeCraft Cash | Cheap CrimeCraft Cash | CrimeCraft Gold | Mytheon Gold | Mytheon Online Gold | Buy Mytheon Gold | Cheap Mytheon Gold | Tales of Pirates Gold | TOP Gold | Buy Tales of Pirates Gold | Buy Talisman Gold | Cheap Talisman Online Gold | Talisman Gold | Talisman online Gold | Talisman currency | Everquest 2 gold | buy eq2 gold | EQ2 Plat | EQ2 Money | Martial Empires | buy martial empires gold | Cheap Martial Empires Gold | All Points Bulletin cash | APB Cash | All Points Bulletin Gold | APB Gold | Pet Forest Gold | Pet Forest Money | Cheap Pet Forest Gold | Land Of Chaos Online Gold | Loco gold | cheap loco gold | Land of Chaos Gold | Soul Master Gold | Soul Master Money | Buy Soul Master Gold | Cheap Soul Master Gold | Tales of Pirates 2 Gold | TOP II Gold | Tales of Pirates II Gold |
leash turkey
leash turkey
re
Some time before, I needed to buy a good house for my business but I did not earn enough money and could not buy anything. Thank heaven my friend adviced to try to take the business loans at trustworthy creditors. Thence, I did so and was satisfied with my collateral loan.
I would like to apply a
I would like to apply a similar kind of effect, where the text 'enter search terms' becomes highlighted upon clicking inside the search box - thus the text is not automatically cleared upon click - but user typing anything will ofcourse naturally overwrite .
You rule.
Thanks for taking the time to put this up. Guys like you that take the time to put up little guides like this save others of us hours upon hours and more than a few handfuls of hair.
Keep it up!
I'm glad you like it. I need
I'm glad you like it. I need to put up some more, but have not had the time lately. The encouragement might help me make some time.
Also, some of these tricks I write about might have taken me hours and hours and more than a few handfuls of hair to figure out, so I figure others will want to use it too.
Thnaks for your overview
I would like to apply a similar kind of effect, where the text 'enter search terms' becomes highlighted upon clicking inside the search box - thus the text is not automatically cleared upon click - but user typing anything will ofcourse naturally overwrite.
The reason for taking this approach is that I often have to search for related terms, i.e. first query may be 'digital cameras' then later I may search 'digital camera accessories' - so with a clearing effect I have to write everything out again - whereas with a text highlight function I can just type to clear everything or double click to deselect the highlighting and append extra terms to the search.
Clear box on included page
Hi there.
I love the simplicity of your script and it works great, until the text box is on a page which is included onto another one. For example, I have a search box as part of a site's border, which is an included page. The text does not clear when you click on the included text box page. Is there an easy "tweak" to fix this?
Genevieve.
Send example
It should work from within a server side include because all the SSI does is combine the pages onto one page. Can I look at the code? Maybe I can see the problem then. If you don't want to post all the code here, use my contact page. Alternatively, a link to the site where I can read the source might work.
Jason
Thanks for the tip. However a
Thanks for the tip. However a bit ironic that when I click in the 'Your name:' field in your comment form, the value "Anonymous" does not clear.
Thanks, I never see the form
Thanks, I never see the form as you do, so I didn't notice that. I'll look at it.
Jason
Post new comment