2

Currently I am recording all the changes made by Jquery as this:

$.fn.meChangeText = function ($text,$textpath) {
    $(this).html($text);
        //I am storing the code block here storing in a string
    $codeblock = $codeblock + "\n" + 
                     "$(\"" + $textpath + "\").html(\"" + $text + "\");"

};

Is there another way where I can convert $(this).html($text) function to a string? This means let say I am changing:

    $("#id1").text("I am changing");

I want it to be in a string as this:

 var code = "$(\"#id1\").text(\"I am changing\");"

Update

Why am I doing this?

This is because I want to store all the changes made in a database.

7
  • Can you explain more about converting to string? Commented Aug 12, 2013 at 8:55
  • Why do you need it? because if its for debugging purposes, you'll be better off with 'console.log' Commented Aug 12, 2013 at 9:00
  • It is not for debugging purpose. It is for me to store the edits. I want to execute the codes that are stored in the string when a user needs to see the changes that he has made. Commented Aug 12, 2013 at 9:02
  • The reason I need this because, I am storing the Jquery code block in the database. Commented Aug 12, 2013 at 9:05
  • It's not trivial to serialize DOM elements. What if an element does not have an ID? You really should edit your question and provide a proper description of what you are trying to do and why (with a better example). Commented Aug 12, 2013 at 9:05

2 Answers 2

4

Just store texts in an array:

history = {}

$.fn.meChangeText = function (text, textpath) {
    if(!history[textpath])    
        history[textpath] = [];
    history[textpath].push($(textpath).html());
    $(textpath).html(text)
})

and the undo function will be like this:

$.fn.undo = function (textpath) {
    if(!history[textpath])    
        return
    $(textpath).html(history[textpath].pop());
})
Sign up to request clarification or add additional context in comments.

3 Comments

The above example is just a text example. It has to be stored in a text string as I will also be changing the css of an element. The above question was simplified. I am not looking at storing the text but storing the code block.
@madi: well, this sounds like XY problem to me. Metaprogramming and code generation is almost never a solution.
I have updated my question. And I hope that I made it clear. The reason is because I want to store all the edits as a code block so that I can run the script when user reverts back to a certain revision.
0

This is my solution and I believe that there is a better solution out there as compared to mine:

 $.fn.ChangeText = function ($text,$textpath) {
          $(this).html($text); //change the textbox
          $temp = '$(this).html($text);'; //make a copy of the change in String
          $temp = $temp.replace("this",'\"'+$textpath+'\"');
          $temp = $temp.replace("$text",'\"'+$text+ '\"');
          $data.code = $data.code + $data.code;
};

However the above solution does not convert the codeblock to String but "hard cording" the method signature as String. So to convert this method signature as String $(this).html($text); I had to "hardcode" the method signature as String as such$temp = '$(this).html($text);';`.

If you have a better solution, please share so that others can benefit.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.