5

Suppose I have a string <span class="msg">Text goes here</span>.I need to use this string as a HTML element in my webpage. Any ideas on how to do it?

Sachin
  • 40,216
  • 7
  • 90
  • 102
Mohit
  • 51
  • 3
  • 1
    It's totally clear what the author is asking. Further votes to close under that heading should try asking if they still don't get it. – Barney May 26 '15 at 02:54

3 Answers3

11

Mithril provides the m.trust method for this. At the place in your view where you want the HTML output, write m.trust( '<span class="msg">Text goes here</span>' ) and you should be sorted.

Daryn
  • 4,791
  • 4
  • 39
  • 52
Barney
  • 16,181
  • 5
  • 62
  • 76
0

Mithril it's powerfull thanks to the virtual dom, in the view you if you want to create a html element you use:

m("htmlattribute.classeCss" , "value");

So in your case:

m("span.msg" , "Text goes here");
Irrech
  • 1,277
  • 2
  • 13
  • 18
-3

Try creating a container you wish to hold your span in.
1. Use jQuery to select it.
2. On that selection, call the jQuery .html() method, and pass in your HTML string.
($('.container').html(//string-goes-here), for example)

You should be able to assign the inner HTML of the container with the string, resulting in the HTML element you want.

Docs here.

Sze-Hung Daniel Tsui
  • 2,282
  • 13
  • 20
  • Fwiw, seems like you're using Mithril. This answer uses basic jQuery. I'm not sure if Mithril has HTML-to-element conversion, but this should solve your problem if you're willing to use jQuery. – Sze-Hung Daniel Tsui May 25 '15 at 10:45
  • 1
    This question is taged as a mithril.js question not a jQuery question. – Alexx Roche Oct 19 '16 at 13:32