code_junkie

Change an element's onfocus handler with Javascript?

Nov 12th, 2011
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. function clear_input(element)
  2. {
  3. element.value = "";
  4. element.onfocus = null;
  5. }
  6.  
  7. function restore_default(element)
  8. {
  9. if(element.value == '')
  10. {
  11. element.value = element.name.substring(0, 1).toUpperCase()
  12. + element.name.substring(1, element.name.length);
  13. }
  14. }
  15.  
  16. element.onfocus = "javascript:clear_input(this);";
  17.  
  18. element.onfocus = clear_input;
  19.  
  20. element.onfocus = function () {
  21. clear_input( param, param2 );
  22. };
  23.  
  24. function clear_input () {
  25. this.value = "";
  26. this.onfocus = null;
  27. }
  28.  
  29. function trim(str) {
  30. return str.replace(/^ss*/, '').replace(/ss*$/, '');
  31. }
  32.  
  33. function capitalize(str) {
  34. return str.substr(0, 1).toUpperCase() + str.substr(1).toLowerCase();
  35. }
  36.  
  37. function clear_default(element) {
  38. if (trim(element.value) == element.defaultValue ) { element.value = ""; }
  39. }
  40.  
  41. function restore_default(element) {
  42. if (!trim(element.value).length) { element.value = element.defaultValue;}
  43. }
  44.  
  45. function hightlight_input(element) {
  46. element.select();
  47. }
  48.  
  49. function restore_default(element) // optional, do we restore if the user deletes?
  50. {
  51. if(element.value == '')
  52. {
  53. element.value = element.name.substring(0, 1).toUpperCase()
  54. + element.name.substring(1, element.name.length);
  55. }
  56. }
  57.  
  58. <!-- JavaScript
  59. function checkClear(A,B){if(arguments[2]){A=arguments[1];B=arguments[2]} if(A.value==B){A.value=""} else if(A.value==""){A.value="Search"}}
  60. //-->
  61.  
  62. <form method="post" action="search.php">
  63. <input type="submit" name="1">
  64. <input type="text" name="srh" Value="Search" onfocus="checkClear(this,'Search')" onblur="checkClear(this,' ')">
  65. </form>
Add Comment
Please, Sign In to add comment