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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.