I currently am trying to simulate a mailbox and am using materialize in order to build it. Inside of my collection view, I am trying to add cells dynamically, but am unsure on how to proceed with appending html using Javascript.
<div class="row" id="mailBox">
<div class="container col s7">
<div class="collection-header center">
<h4>Email Box</h4>
</div>
<ul class="collection with-header" id="mailCollection">
<!-- Start: Trying to Dynamically Add These Cells To this view -->
<li class="collection-item avatar email-unread">
<span class="circle indigo darken-1"></span>
<span class="email-title"> Subject </span>
<p class="truncate grey-text ultra-small"> Message </p>
<a href="#!" class="secondary-content email-time"><span class="blue-text ultra-small">Date Recieved</span></a>
</li>
<!-- End of Cell Content -->
</ul>
</div>
</div>
I trying to append the collection items as such through my javascript, although nothing is appearing on the screen:
function createHTML(){
var jsonData = jsonObject;
for(var i =0; i < jsonData.length; i++){
$("#mailCollection").append('<li class="collection-item avatar email-unread">' +
'<span class="circle indigo darken-1"></span>'
'<span class="email-title">' + jsonData[0] + '</span>'
'<p class="truncate grey-text ultra-small">' + jsonData[1] + '</p>'
'<a href="#!" class="secondary-content email-time"><span class="blue-text ultra-small">' + jsonData[2] + '</span></a>
</li>')
}
}
As per your code, There are so many syntax errors in single and double quotes where you append the html. Please check below code without any syntax error.
function createHTML(){
var jsonData = jsonObject;
for(var i =0; i < jsonData.length; i++){
$("#mailCollection").append("<li class='collection-item avatar email-unread'><span class='circle indigo darken-1'></span><span class='email-title'>' + jsonData[0] + '</span><p class='truncate grey-text ultra-small'>' + jsonData[1] + '</p><a href='#!' class='secondary-content email-time'><span class='blue-text ultra-small'>' + jsonData[2] + '</span></a></li>")
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row" id="mailBox">
<div class="container col s7">
<div class="collection-header center">
<h4>Email Box</h4>
</div>
<ul class="collection with-header" id="mailCollection">
<!-- Start: Trying to Dynamically Add These Cells To this view -->
<li class="collection-item avatar email-unread">
<span class="circle indigo darken-1"></span>
<span class="email-title"> Subject </span>
<p class="truncate grey-text ultra-small"> Message </p>
<a href="#!" class="secondary-content email-time"><span class="blue-text ultra-small">Date Recieved</span></a>
</li>
<!-- End of Cell Content -->
</ul>
</div>
</div>