Need som help to modify this script.

Need som help to modify this script.

Hi! 


I found this very userfull (edit in place) script and i would like to make som modifications to it but i dont get it to work propper.


What i want to achive is that when you are clicking the "edit" button my <p>hello there</p> becomes <p><input type="text" value="hello there" /></p>


I managed to make it convert to an input by using .replaceWith but a cant get it to change back when i press my save och cancel button.

And i also dont understand how i can include my prevValue var inside value=" prevValue "...


I hope some can help me, here is the code.


  1. /**
  2.  * editableText plugin that uses contentEditable property (FF2 is not supported)
  3.  * Project page - http://github.com/valums/editableText
  4.  * Copyright (c) 2009 Andris Valums, http://valums.com
  5.  * Licensed under the MIT license (http://valums.com/mit-license/)
  6.  */
  7. (function(){
  8. /**
  9. * The dollar sign could be overwritten globally,
  10. * but jQuery should always stay accesible
  11. */
  12. var $ = jQuery;
  13. /**
  14. * Extending jQuery namespace, we
  15. * could add public methods here
  16. */
  17. $.editableText = {};
  18. $.editableText.defaults = {  
  19. /**
  20. * Pass true to enable line breaks.
  21. * Useful with divs that contain paragraphs.
  22. */
  23. newlinesEnabled : false,
  24. /**
  25. * Event that is triggered when editable text is changed
  26. */
  27. changeEvent : 'change'
  28. };  
  29. /**
  30. * Usage $('selector).editableText(optionArray);
  31. * See $.editableText.defaults for valid options 
  32. */
  33. $.fn.editableText = function(options){
  34. var options = $.extend({}, $.editableText.defaults, options);

  35. return this.each(function(){
  36. // Add jQuery methods to the element
  37. var editable = $(this);

  38. /**
  39. * Save value to restore if user presses cancel
  40. * This var will hold previous value
  41. */
  42. var prevValue;

  43. // Create edit/save buttons
  44. var buttons = $(
  45. "<div class='editableToolbar'>" +
  46. "<span class='edit'>Edit</span>" +
  47. "<span class='save'>Save</span>" +
  48. "<span class='cancel'>Cancel</span>" +
  49. "</div>")
  50. .insertBefore(editable);

  51. // Save references and attach events            
  52. var editEl = buttons.find('.edit').click(startEditing);

  53. buttons.find('.save').click(function(){
  54. stopEditing();
  55. editable.trigger(options.changeEvent);
  56. });
  57. buttons.find('.cancel').click(function(){
  58. stopEditing();
  59. editable.html(prevValue);
  60. });

  61. // Display only edit button
  62. buttons.children().css('display', 'none');
  63. editEl.show();

  64. if (!options.newlinesEnabled){
  65. // Prevents user from adding newlines to headers, links, etc.
  66. editable.keypress(function(event){
  67. // event is cancelled if enter is pressed
  68. return event.which != 13;
  69. });
  70. }

  71. /**
  72. * Makes element editable
  73. */
  74. function startEditing(){
  75. // Save previous value               
  76. prevValue = editable.html();

  77. buttons.children().show();
  78. editEl.hide();

  79. editable.replaceWith('<p class="editable"><input type="text" value="" /></p>');
  80. }
  81. /**
  82. * Makes element non-editable
  83. */
  84. function stopEditing(){
  85. buttons.children().hide();
  86. editEl.show();
  87. editable.replaceWith('<p class="editable"></p>');
  88. }
  89. });
  90. }
  91. })();
  92.