You know the search bar on websites that shows some default text like “Search” and when you click on it the text disappears.
Whats the best approach to do it?
Like hard code the “search” value for example:
var val = document.getElementById(‘txtsearch’).value;
if(val == “Search”){
//code to empty the text box
}
If yes then how do you manage when you have to work on a multi-lingual website ? You cannot hard code the value for each lang. Whats the best approach in your opinion?
That method looks OK. There is a thread here with some fuller code:
If you have to change the text based on the language, perhaps you could first detect the lang attribute on the <html> tag, and, based on that, serve up default text to suit.
Post #11 has the full code for the text input. You just need to work out how to set a different default value for each language. I’m not much of a programmer, but I would probably do it with PHP. I guess the first question I’d have for you is—How does your site work now? Do you have separate pages for different languages? If so, do you use the lang= attribute on each page? If so, how is that set up? Manually, or … ?
You can change the value in the input tag itself to change the default value without needing to change the script at all. Similarly you can apply the same code to other fields just by changing the value of the id.
To make it easy to apply to as many fields as you like you can make it a function and pass the id of the field to add the code to:
Another nice solution is to use the new placeholder attribute in HTML (which is supported by the latest browsers). That would also make it much more straightforward to switch the placeholder text depending on the language of the page (using a PHP condition or something).