Question

I'm making a website. When you click on my search box the text goes away, like it's supposed to, but once you click away the text does not come back and I would like it to. Here is my snippet

<div class='search'>
<form method="get" id='search' action="http://www.google.com/search">
<input type="hidden" name="q" size="31" maxlength="255" value="Site Name:" />
<input type="text" name="q" size="31" maxlength="255" style="height: 24px;" value="Search..." onFocus="this.value=''" />
</div>

I have alot of css behind it to make the text box enlarge and change color. Here is the code just in case it's relevant.

#search input[type="text"] {
background: url(imgs/search-white.png) no-repeat 10px 6px #444;
border: 0 none;
font: bold 12px Arial,Helvetica,Sans-serif;
color: #d7d7d7;
width:150px;
padding: 6px 15px 6px 35px;
-webkit-border-radius: 20px;
-moz-border-radius: 20px;
border-radius: 20px;
text-shadow: 0 2px 2px rgba(0, 0, 0, 0.3); 
-webkit-box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 3px rgba(0, 0, 0, 0.2)   inset;
-moz-box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 3px rgba(0, 0, 0, 0.2) inset;
box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 3px rgba(0, 0, 0, 0.2) inset;
-webkit-transition: all 0.7s ease 0s;
-moz-transition: all 0.7s ease 0s;
-o-transition: all 0.7s ease 0s;
transition: all 0.7s ease 0s;
outline: none;
}

#search input[type="text"]:focus {
background: url(imgs/search-dark.png) no-repeat 10px 6px #fcfcfc;
color: #6a6f75;
width: 175px;
-webkit-box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 0 rgba(0, 0, 0, 0.9) inset;
-moz-box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 0 rgba(0, 0, 0, 0.9) inset;
box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 0 rgba(0, 0, 0, 0.9) inset;
text-shadow: 0 2px 3px rgba(0, 0, 0, 0.1);
outline: none;
}
div.search
{
position:fixed;
top:8px;
right:5px;
}

When the box is clicked the box enlarges and the Search... text goes away like it's supposed to. But once you click away the text does not return and I would like it to. How do I do that? *side note sorry if website looks bad im 13

Was it helpful?

Solution

Change:

<input type="text" name="q" size="31" maxlength="255" style="height: 24px;" value="Search..." onFocus="this.value=''" />

for:

<input type="text" name="q" size="31" maxlength="255" style="height: 24px;" placeholder="Search..." onFocus="this.value=''" />

Just need to change value to placeholder :)

OTHER TIPS

If you want it to be compatible with older browsers (ones that do not support html5) you could do something like this:

    function removePlaceholder(element)  {
        if (element.value == "Search...") {
            element.value = "";
        }
    }

    function replacePlaceholder(element)  {
        if (element.value == "") {
            element.value = "Search...";
        }
    }

And then set your input tag to this:

<input type="text" name="q" size="31" maxlength="255" style="height: 24px;" value="Search..." onBlur = "replacePlaceholder(this)" onFocus="removePlaceholder(this)" />

Hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top