dennismonsewicz Posted February 3, 2009 Share Posted February 3, 2009 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? Link to comment https://forums.phpfreaks.com/topic/143621-text-and-input-fields/ Share on other sites More sharing options...
KevinM1 Posted February 3, 2009 Share Posted February 3, 2009 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 https://forums.phpfreaks.com/topic/143621-text-and-input-fields/#findComment-753581 Share on other sites More sharing options...
Psycho Posted February 3, 2009 Share Posted February 3, 2009 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 https://forums.phpfreaks.com/topic/143621-text-and-input-fields/#findComment-753824 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.