4

I am trying to reload a DIV that has the following in it from before.

<div id="mydiv">{% include "comments/comments.html" %}</div>

What I want to do is this when I click a button;

$('#mydiv').html('{% include "comments/comments.html" %}</div>

But putting the django tag inside the jquery html function makes javascript very unhappy. I know that this can be solved with .load() but that would mean I would have to rewrite my entire logic which would break other stuff.

2
  • 2
    Any jQuery calls will happen after the DOM is loaded. While any include statements will happen before. So this approach will not work. If you don't want to use $.load() you could include into a hidden element, then use jQuery to .show(). I'd use .load(). Commented Jan 22, 2014 at 16:14
  • Thanks. Makes sense. I was hoping that it was possible to "re-include" it with jQuery. Guess I'll just have to rework my applogic. Commented Jan 22, 2014 at 20:50

1 Answer 1

1

Use Template Literals and given your post date, they would have been called Template Strings in ES5.

Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them. They were called "template strings" in prior editions of the ES2015 specification.

Template literals

JQuery keeps breaking when you paste the HTML code from the HTML file because it takes in a string, not HTML, and putting " or ' around {% include ... %} will break from any of the same characters in your comments.html. Put backticks around your comments.html file then import it with Django in a JQuery method.

`<div id='mainpanel{{ i.id }}' class='panel panel-default'>
   <div class='panel-heading clearfix'>
      <b class='panel-title'>{{ i.itemfullname|escapejs }}</b>
      <a id='close{{i.id}}' class='btn btn-default btn-sm pull-right'>X</a>
   </div>
<div>`
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.