Trix: Get the html string

Created on 6 Jul 2016  路  1Comment  路  Source: basecamp/trix

Hello,

My question is that I want to get the html code for the string that the user has written in the text area.
For example:
This is an <b>example</b>

But I get this:

"text":[{"text":[{"type":"string","attributes":{"bold":true},"string":"Rgg v"},{"type":"string","attributes":{"blockBreak":true},"string":"\n"}],"attributes":[]}]

My code is (the var doc returns me the code above):

      <script type="text/javascript">

        document.addEventListener('trix-change', function(e){

            var element = document.querySelector("trix-editor");
            var doc = element.editor.getDocument();
        });

      </script>
Details
  • Trix version: Latest
  • Browser name and version: Default borwser
  • Operating system: Android 4.4.2

Most helpful comment

You can get the HTML from the <trix-editor>'s value property:

document.querySelector("trix-editor").value  // "<div>This is an <strong>example</strong></div>"

In a Trix event handler:

document.addEventListener("trix-change", function(event) {
  var element = event.target
  var html = element.value  // "<div>This is an <strong>example</strong></div>"
})

>All comments

You can get the HTML from the <trix-editor>'s value property:

document.querySelector("trix-editor").value  // "<div>This is an <strong>example</strong></div>"

In a Trix event handler:

document.addEventListener("trix-change", function(event) {
  var element = event.target
  var html = element.value  // "<div>This is an <strong>example</strong></div>"
})
Was this page helpful?
0 / 5 - 0 ratings

Related issues

madikarizma picture madikarizma  路  5Comments

lcsqlpete picture lcsqlpete  路  3Comments

lanzhiheng picture lanzhiheng  路  4Comments

plainspace picture plainspace  路  4Comments

andreimoment picture andreimoment  路  3Comments