Searching for an HTML to Javascript parser?

Hi there,

I’m looking for some app, can be online, can be free or not
that can process some html code to output the corresponding javascript to create the html.
for exemple :

<ul class="list-group">
  <li class="list-group-item">Cras justo odio</li>
  <li class="list-group-item">Dapibus ac facilisis in</li>
  <li class="list-group-item">Morbi leo risus</li>
  <li class="list-group-item">Porta ac consectetur ac</li>
  <li class="list-group-item">Vestibulum at eros</li>

would become

el.className += "list-group";
el.createTextNode("Cras justo odio");

but not something like :

document.write "<ul class="l"ist-group""> ";
document.write "<li class ...

does such tool exist ?

You can actually use jQuery to do this easily:

var elementToAdd = $('<ul class="list-group"></ul>');

You can add child elements to the string, etc. This will create a jQuery object representing the DOM element which you can then append to a parent. Let’s say you have a parent element with ID “DfeaQW”:

var parentEl = $('#DfeaQW');
var elementToAdd = $('<ul class="list-group"></ul>');

The main things to watch out for with this method are EOLs (replace or remove them), and quote mixing.

1 Like