1

Yes I have see the post here

And I tried that but the problem is, my jquery object looks more like this:

var $foo = $('<ul><li id="' + line.id + '" label="' + label + '" rel="file"><a href="#">' + line.title + '</a></li></ul>');
$foo.click(function() { openLink(line.url) });
$foo.appendTo($myDiv);

When $myDiv is fully populated I can do this:

var html = $('<div>').append($('#foo').clone()).remove().html();

And I will see all of the lovely HTML, but I don't know if the click stuff will be preserved. See, I want to save the entire DOM modification to localStorage so I can retrieve it quickly since it's pretty static. I need to be able to store it and all its attributes, then yank it back out and restore it, clicks and all.

Does that make sense?

1
  • You cannot save a function as a string. Commented Feb 7, 2011 at 14:59

1 Answer 1

3

The only way to do this would be to use inline event handlers, which is a bad (and slow) idea.

Instead, you can convert all of your event handlers to live handlers; they will then automatically apply to all matching elements without having to rebind them after changing the DOM.

Sign up to request clarification or add additional context in comments.

5 Comments

I agree. You could identify which to bind with a class name, and store your line.url in an attribute directly on the UL or whatever you want to be clicked.
Definitely the way to go here. Check out the official documentation for implementation details.
Oh nice. So instead of "$foo.click(function() { openLink(line.url) });" I would implement a more dynamic "live" handler? I will try that! Awesome, thanks!
One quick question, how would I go about saving line.url as an attribute directly on the URL and accessing it in my .live handler?
@user: $(this).getAttr("url")

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.