Using jQuery to alter the search form

I had someone today request that the search form on their site contain the word search and for that word to disappear once the user clicked on it (onfocus). I have set this sites search to do that for now.

On prior plain or other non Drupal sites I'd usually just set the value in the field and add an onfocus to clear it. Not wanting to hook the search form a quick jQuery addition to the template not only worked but didn't have the issues of browsers with out javascript enabled.

To the template.php I added this (Note: the input id: #edit-search-block-form-1 may be different for other sites. It was #edit-search-block-form-3 for the site I did this on)

 

drupal_add_js("$(document).ready(function () {
  $('#edit-search-block-form-1').val('search');
  $('#edit-search-block-form-1').focus(function() {
    $(this).val('');
  });
});", 'inline');

 

 Also usable on non Drupal sites (assuming jQuery is loaded with the page and entered the input ID) -

<script type="text/javascript"><![CDATA[//><!--

$(document).ready(function () {
  $('#edit-search-block-form-1').val('search');
  $('#edit-search-block-form-1').focus(function() {
    $(this).val('');
  });
});

//--><!]]>

</script>

 

Comments

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
6 + 13 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.