1

Why cant I append pasted html with appendChild? This is my code:

<div id="byeeee"></div>

var appenddescription = $.parseHTML("<h1>hello</h1> bye<br>");

document.getElementById("byeeee").appendChild(appenddescription);

My goal is to parse the html and append it to the div. When I try the code above I get the following error:

Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.

I tried to use this answer https://stackoverflow.com/a/31800768/15677215 but it didnt help

P.S: I'm using Jinja as a templating engine. In my real code the JavaScript would look like

var appenddescription = $.parseHTML("{{hello}}");
mcdonalds291
  • 2,024
  • 1
  • 5
  • 14

1 Answers1

1

If you're going to use jQuery, it's discouraged to mix it with JS. Use jQuery's .append() like so:

var appenddescription = $.parseHTML("<h1>hello</h1> bye<br>");
$("#byeeee").append(appenddescription);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="byeeee"></div>

To turn off HTML escaping in Jinja, use:

var appenddescription = $.parseHTML("{{hello|safe}}");

jQuery.parseHTML() | jQuery API Documentation

Spectric
  • 30,714
  • 6
  • 20
  • 43
  • 1
    Hm, not sure why. But when I try this on my website it says ``

    hello

    bye
    `` (literally) the
    's dont make a new line. If it helps, Im using the templating engine Jinja to generate whats in parseHTML() . Appending does work though
    – mcdonalds291 May 16 '21 at 03:55
  • 1
    @mcdonalds291 That's odd... I'm not familiar with that templating engine but does it by any chance escape HTML tag start/ends? – Spectric May 16 '21 at 03:56
  • 1
    I dont believe it does. I will post a screenshot of how it looks when using inspect element. – mcdonalds291 May 16 '21 at 03:57
  • 1
    Here is that screenshot https://i.imgur.com/PVodJFw.png – mcdonalds291 May 16 '21 at 03:59
  • 1
    @mcdonalds291 If dev tools show the literal string, then that's an indicator that the string is being escaped. Try double clicking the text, which should reveal whether the `<` characters are escaped. – Spectric May 16 '21 at 04:01
  • 1
    How would I see if the ``<`` characters are escaped? I dont see any ``\``'s – mcdonalds291 May 16 '21 at 04:02
  • 1
    @mcdonalds291 Right click the element and click 'Edit as HTML'. Does it look contain things like this? `<tagname>` – Spectric May 16 '21 at 04:03
  • 1
    Yes! It does. When I edit as HTML it shows ``hello <br> <br> <h1>bye</h1>``(This is why I thought parseHTML wasn't working) – mcdonalds291 May 16 '21 at 04:04
  • 1
    @mcdonalds291 In that case, [turning off auto-escaping in Jinja](https://stackoverflow.com/questions/3206344/passing-html-to-template-using-flask-jinja2) might be the way to go :) – Spectric May 16 '21 at 04:07
  • 1
    @mcdonalds291 I've updated my answer for Jinja. It should work now – Spectric May 16 '21 at 04:08
  • 1
    Yess! Thank you so much! It works flawlessly. To anyone who is reading this discussion, make sure you trust the input your injecting into your code before using | safe – mcdonalds291 May 16 '21 at 04:09
  • 1
    @mcdonalds291 Glad to help :) Please consider accepting if it solves your question. – Spectric May 16 '21 at 04:10