0

I am trying to modify an HTML string (things like adding class to one of its children). In my code I have to used a container as a midway to ouput $html as a string. Does jQuery provide any function to do this?

  html = "<p>title</p><div><ul class='www'></ul>something</div>";
  $html = $(html);
  $html.filter('div').find('ul').addClass('xxx');
  container = $('<div></div>');
  html = container.html($html)[0].innerHTML; //output "<p>title</p><div><ul class='www xxx'></ul>something</div>"
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
shenkwen
  • 3,536
  • 5
  • 45
  • 85

1 Answers1

0

Nope.

There is no escape to interacting with the DOM (this is creating or selecting an existing element), the best you can try is to document.write your string but you'll need to scape the HTML so it doesn't gets interpreted as HTML but text. Notice that document.write only works before the document finishes loading.

I don't know your needs but I can't think of a good use case for this, your jQuery should be better for most cases.

martriay
  • 5,632
  • 3
  • 29
  • 39