Search code examples
jqueryhtmldynamiccode-generationauto-generate

Auto-generate jQuery script from HTML


Is there any tool / library which would:

  1. Take HTML markup as input
  2. Auto-generate a jQuery script which, in return, if executed, would
  3. Generate the original HTML?

Solution

  • As I wrote in comment, It depends on how complex your HTML is.

    If there's something pretty simple, then

    $('<div class="content">...</div>')
    

    will return your HTML as DOM element already wrapped in jQuery. Otherwise (if you have scripts, events etc) you'll need to use $.parseHTML (as mentioned by @emergence) and pass extra arguments.

    Here's official doc for .parseHTML: http://api.jquery.com/jquery.parsehtml/