30
You can render HTML using document.write()
document.write('<html><body><h2>HTML</h2></body></html>');
But to append existing HTML string, you need to get the id of the node/tag under which you want to insert your HTML string.
There are two ways by which you can possibly achieve this:
- Using DOM β
var tag_id = document.getElementById('tagid');
var newNode = document.createElement('p');
newNode.appendChild(document.createTextNode('html string'));
- Using innerHTML β
var tag_id = document.getElementById('tagid');
tag_id.innerHTML = 'HTML string';
9
Use $.parseHTML before the append html like as
var html = '<div class="col-lg-4 col-references" idreference="'+response+'"><span class="optionsRefer"><i class="glyphicon glyphicon-remove delRefer" style="color:red; cursor:pointer;" data-toggle="modal" data-target="#modalDel"></i><i class="glyphicon glyphicon-pencil editRefer" data-toggle="modal" data-target="#modalRefer" style="cursor:pointer;"></i></span><div id="contentRefer'+response+'">'+refer_summary+'</div><span id="nameRefer'+response+'">'+refer_name+'</span></div>';
html = $.parseHTML( html);
$("#references").append(html);
- [Django]-Django "get() got an unexpected keyword argument 'pk'" error
- [Django]-Overriding the save method in Django ModelForm
- [Django]-Redirect to Next after login in Django
5
You can use Javascriptβs document.createRange().createContextualFragment
:
const frag = document.createRange().createContextualFragment('<div>One</div><div>Two</div>');
console.log(frag);
/*
#document-fragment
<div>One</div>
<div>Two</div>
*/
- [Django]-Django check if object in ManyToMany field
- [Django]-Adding a ManyToManyWidget to the reverse of a ManyToManyField in the Django Admin
- [Django]-How do I match the question mark character in a Django URL?
2
If you want to do it in ReactJS, you can use "dangerouslySetInnerHTML" attribute, instead of "innerHTML" attribute.
As it allows the attackers to inject codes (XSS), it is named dangerous!
const myHtmlData = `
<ul>
<li>Item1<li>
<li>Item2<li>
</ul>`;
...
<div
dangerouslySetInnerHTML={{
__html: myHtmlData
}}>
- [Django]-Django default=timezone.now + delta
- [Django]-Django sending email
- [Django]-Fighting client-side caching in Django
0
You need to assign a particular div an id and then process/change UI in the specific div accordingly.
Given here is an excellent explanation.
- [Django]-Django: best practice for splitting up project into apps
- [Django]-Including a querystring in a django.core.urlresolvers reverse() call
- [Django]-How to upload multiple files in django rest framework
Source:stackexchange.com