Reputation: 1925
Please check out the jsfiddle link below:
As you can see it is two search boxes with a button below it. How can I have it so text is in each box but when each box is clicked the text disappears?
Also if the box is clicked but nothing is entered the text reappears.
Thanks guys, I have been searching but I am unsure of what this is called or what language needs to be used.
Thanks again!
James
Upvotes: 1
Views: 1742
Reputation: 67194
IF you're using HTML5 you can use the new placeholder=""
attribute:
placeholder="Your text here"
If you're not using HTML5, you can use jQuery to change the value: (for older browsers and IE)
$('input').focus(function(){
// on focus if the value of the field is still the initial then clear it
if ( (this.value) == this.defaultValue ) this.value = ''; }).blur(function(){
// on blur if the value is still cleared then restore the initial
if ( (this.value) == '' ) this.value = this.defaultValue; })
Upvotes: 2
Reputation: 3928
Placeholder is what you're looking for.
<input type="text" name="searchText" placeholder="Search Here!" />
See W3Schools for information regarding the placeholder attribute.
Upvotes: 0
Reputation: 2985
This link offers support on the placeholder attribute.
Its not completely cross browser compliant at the moment, IE is the main issue.
http://davidwalsh.name/html5-placeholder-css
For IE people are making divs above them and when you click it hides. There are plenty of options on google by typing in placeholder :)
Hope this helps!
Upvotes: 0
Reputation: 165941
If you need to support older browsers and can't use the HTML5 placeholder
attribute, you will need to use JavaScript. Something along these lines should get you started:
var input = document.getElementById("input1");
input.onfocus = function() {
if(this.value === "Default") {
this.value = "";
}
};
input.onblur = function() {
if(this.value === "") {
this.value = "Default";
}
};
Here's an updated fiddle to show you how it works. Note that I've added an id
to the input
element so it can be accessed with getElementById
. If you want to apply this to multiple input elements, you may want to generalise it instead of applying it to just the one element.
Upvotes: 1
Reputation: 103135
I used infieldlabel to accomplish this on a page once and it worked well. Hoever, it is a jQuery plugin.
Upvotes: 0
Reputation: 8706
Use placeholder attribute on your inputs.
<input type='text' placeholder='whatever' />
Upvotes: 2