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>
</ul>

would become

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

but not something like :

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

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>');
parentEl.append(elementToAdd);

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

1 Like