background image
HomeRecent PostsDrupalSearchTagsRSSContactAboutAccount
Eric.London's picture

Here's a quick way to modify the Drupal search form using a few lines of jQuery. Normally, I would implement a form_alter hook, but let's start simple in this example:

<?php
$(document).ready(function(){
 
// set the text of the search button
 
$('div#search input#edit-submit').attr('value','GO');

 
// add some default verbiage to the text input and have it disappear when the user focuses on it
 
var searchString = 'Search...';
  $(
'div#search input#edit-search-theme-form-keys').val(searchString);
  $(
'div#search input#edit-search-theme-form-keys').focus(function(){
    if ($(
this).val()==searchString) $(this).val('');
  });
});
?>


Update: Here's an updated/alternative version of this code snippet which also has a blur() event:

<?php
$(document).ready(function(){

 
// set the text of the search button
 
$('form#search-block-form input#edit-submit').attr('value','GO');

 
// add some default verbiage to the text input and have it disappear when the user focuses on it
 
var searchString = 'Search...';
   
 
// set input value (text)
 
$('form#search-block-form input#edit-search-block-form-keys').val(searchString);
   
 
// focus
 
$('form#search-block-form input#edit-search-block-form-keys').focus(function(){
    if ($(
this).val()==searchString) $(this).val('');
  });
   
 
// blur
 
$('form#search-block-form input#edit-search-block-form-keys').blur(function(){
    if ($(
this).val()=='') $(this).val(searchString);
  });
   
});
?>

Eric.London's picture

To improve usability, I wanted to place the user's cursor on the first form input of each page when the document loads. Here's the JQuery I used...

<?php
$(document).ready(function(){
  $(
"input").not("[@type='hidden']")[0].focus();
});
?>

Syndicate content