nikola Posted May 3, 2012 Share Posted May 3, 2012 How can I add default value to search form input field, for instance: value="Enter your search term" when using: <?php echo htmlentities($input->whitelist('term'), ENT_QUOTES, 'UTF-8'); ?> that "occupies" input field value. That default value ("Enter your search term") would have to be excluded from the search. The behaveiour I'm looking for is similar to search field in PW admin that has value "Search"... Link to comment Share on other sites More sharing options...
DaveP Posted May 3, 2012 Share Posted May 3, 2012 There are a few options, including Javascript <form action="SCRIPT-NAME" method="post"> <input type="text" value="Search..." name="search" onfocus="if (this.value == 'Search...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Search...';}" /> </form> and in HTML5 <input type="text" name="search" placeholder="Search..." /> which works in most up to date browsers (but I think not IE9, although I haven't checked recently). Link to comment Share on other sites More sharing options...
yellowled Posted May 3, 2012 Share Posted May 3, 2012 which works in most up to date browsers (but I think not IE9, although I haven't checked recently). There are polyfills for the HTML5 placeholder attribute (i.e. pieces of usually javascript to replicate a certain feature in browsers who do not provide it natively). The Modernizr wiki on GitHub has a pretty comprehensive list of them. Personally, I think placeholder doesn't need to be polyfilled, it's fine to not have it in older browsers. If that's not feasible, the polyfill by Mathias Bynens works pretty good. 1 Link to comment Share on other sites More sharing options...
nikola Posted May 3, 2012 Author Share Posted May 3, 2012 Thank you both, I ended up with a small bit of jQuery: $('input[name=search]').attr({value: 'Search'}); Link to comment Share on other sites More sharing options...
yellowled Posted May 3, 2012 Share Posted May 3, 2012 That will probably perform a search with the search term "Search" if somebody submits the search form without having entered actual search terms. This is not always desirable, and it does not happen if you use the placeholder attribute. (Just sayin'.) Link to comment Share on other sites More sharing options...
DaveP Posted May 3, 2012 Share Posted May 3, 2012 And you could do the same without jQuery just by adding value="Search" to the <input> declaration. (Just sayin'.) Link to comment Share on other sites More sharing options...
nikola Posted May 3, 2012 Author Share Posted May 3, 2012 I ended up using this script: http://webcloud.se/code/jQuery-Placeholder and it does exactly what I needed. Link to comment Share on other sites More sharing options...
AnotherAndrew Posted May 3, 2012 Share Posted May 3, 2012 I have tried all of these suggestions and it doesn't work....Unhappy. Link to comment Share on other sites More sharing options...
nikola Posted May 3, 2012 Author Share Posted May 3, 2012 If you use search form from default PW template, you could do the following with the above script mentioned. <input type='text' name='q' id='search_query' value='<?php echo htmlentities($input->whitelist('q'), ENT_QUOTES, 'UTF-8'); ?>' /> Add HTML5 placeholder to input field like this: <input type='text' name='q' id='search_query' placeholder='Search' value='<?php echo htmlentities($input->whitelist('q'), ENT_QUOTES, 'UTF-8'); ?>' /> Include the script above to your head section and trigger it like this (also in the head): <script> $(document).ready(function() { $('input[name="q"]').placeholder(); }); </script> Link to comment Share on other sites More sharing options...
AnotherAndrew Posted May 3, 2012 Share Posted May 3, 2012 Thanks Nikola. Turns out that I was not calling the correct character set in my meta tag. And all of the suggested methods above work now. Link to comment Share on other sites More sharing options...
yellowled Posted May 3, 2012 Share Posted May 3, 2012 I ended up using this script: http://webcloud.se/c...ery-Placeholder and it does exactly what I needed. I have never used that particular one, but just from experience: placeholder polyfills can behave erratically in some form environments. Some of them use relative/absolute positioning to overlay the input element with a span which can have various side effects depending on the rest of the CSS. So always make sure you test this properly, it can come back and bite you. As always, all this depends on which browsers you have to/want to support. Then again, if you don't have to support old IE, polyfills are usually not necessary in the first place. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now