Jump to content

text and input fields


dennismonsewicz

Recommended Posts

I have a search field that has a preset value and I have a javascript onclick that erases that preset text but how do you reset the preset value once the user has clicked off of the search field?

 

If it's a form field, attaching an event handler that resets the default search value to onblur should do the trick.

Here is a watered down version of a script I have

 

<html>
<head>
<script type="text/javascript">

window.onload = function()
{
  searchFocus(document.getElementById('search'), false);
}

function searchFocus(fieldObj, focusBool)
{
  //trim the field value
  fieldObj.value = fieldObj.value.replace(/^\s+|\s+$/g,'');
  //See if the prompt text has been defined
  if (fieldObj.getAttribute('prompt'))
  {
    promptText = fieldObj.getAttribute('prompt');
    //If onfocus & value equals the prompt text
    if (focusBool && fieldObj.value==promptText)
    {
      fieldObj.value = '';
    }
    //If onblur & value is empty
    else if (!focusBool && fieldObj.value.replace(/^\s+|\s+$/g,'')=='')
    {
      fieldObj.value = promptText;
    }
  }
  return;
}
</script>
</head>
<body>
<input type="text" name="search" id="search" onfocus="searchFocus(this, true);" onblur="searchFocus(this, false);" prompt="Enter search string" />
<br />
<input type="text" name="otherfield" />
</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.