JavaScript Materialize.css Modal is Not Initializing Correctly on Dynamically Created Elements materialize modal initialize,materialize modal

I'm using an ajax call to append rows dynamically to a table using jQuery. I would like the button generated in each row to display a modal with data specific to the items in that particular row.

I am currently running into the following issue:

-The modal appears when I click on the 1st & 2nd buttons. However, when I try to close out of the 2nd modal, the box shadow from the modal does not go away, and I can no longer continue clicking on any buttons until I refresh the page.

I checked the Materialize.css documentation and it mentions that we need to initialize the modal with $(".modal-trigger").leanModal() if using a trigger to show a modal. The ID that I am using to trigger the modal is called "nutrition-facts" in the code below.

Any help would be appreciated. Thanks!!

UPDATE: With CodePen: http://codepen.io/mtaggart89/pen/pgbgOB

HTML:

<!-- Modal Structure -->
<div id="nutrition-facts" class="modal modal-fixed-footer">
  <div class="modal-content">
    <h4>Nutrition Facts</h4>
    <p>A bunch of text</p>
  </div>
  <div class="modal-footer">
    <a target='_blank' href="#!" class="modal-action modal-close waves-effect waves-green btn-flat">Close</a>
  </div>
</div>

jQuery Function to Create Elements:

function buildTable(foodData) {
  var itemList = foodData.list.item;
  var foodGroup, foodName, newDiv, createButton, ndbNumber, createTable, tableHead, categoryHeading, nameHeading, tr;
  $("table").addClass("bordered");
  categoryHeading = $("<th>").html("Category");
  nameHeading = $("<th>").html("Name");
  $("thead").append(categoryHeading).append(nameHeading);       
  for (var i = 0; i < itemList.length; i++) {
    foodGroup = $("<td>").html(itemList[i].group);
    foodName = $("<td>").html(itemList[i].name);
    ndbNumber = itemList[i].ndbno;
    newDiv = $("<td>");
    createButton = $("<a>")
                    .addClass("waves-effect waves-light btn cyan nutrition modal-trigger")
                    .attr("href", "#nutrition-facts")
                    .html("Nutrition Facts")
                    .attr('data-ndbnum', ndbNumber);
    addButton = newDiv.append(createButton);
    tr = $("<tr>").append(foodGroup).append(foodName).append(addButton);
    $("tbody").append(tr);
  }
}

jQuery Event Listener:

$(document).on('click', '.nutrition', function(e){
  e.preventDefault();
  var ndbNumber = $(this).attr('data-ndbnum');
  $(".modal-trigger").leanModal();
});

http://materializecss.com/modals.html

Answer:1

I need to upload an from a clients computer to the server running the code. It wouldn't be hard if those files were less than 2mb, but since the file may be a video as well, I'm not sure how to ...

I need to upload an from a clients computer to the server running the code. It wouldn't be hard if those files were less than 2mb, but since the file may be a video as well, I'm not sure how to ...

  1. upload file image php
  2. upload file image laravel
  3. upload file image html
  4. upload file image using ajax
  5. upload file image codeigniter
  6. upload file image javascript
  7. upload file image bootstrap
  8. upload file image
  9. upload file image jquery
  10. upload file image angular
  11. upload file image only
  12. upload file image react
  13. upload file image php mysql
  14. upload file image c#
  15. upload file image python
  16. upload file image icon
  17. upload file image angularjs
  18. upload file image type
  19. upload file image yii
  20. upload file image input

I am trying to create a table with non repeating random letters (uppercase only) using JavaScript. Someone posted an answer, but it was deleted before I got a chance to ask: 1) Why is there a var x ...

I am trying to create a table with non repeating random letters (uppercase only) using JavaScript. Someone posted an answer, but it was deleted before I got a chance to ask: 1) Why is there a var x ...

Probably not the best question ever, but I have a doubt here. Is there any difference writing this: var myFunction = function myFunction () {}; myFunction.instance = null; and this: var myFunction ...

Probably not the best question ever, but I have a doubt here. Is there any difference writing this: var myFunction = function myFunction () {}; myFunction.instance = null; and this: var myFunction ...

  1. javascript anonymous function with parameters
  2. javascript anonymous function this
  3. javascript anonymous function vs named
  4. javascript anonymous function return
  5. javascript anonymous function arrow
  6. javascript anonymous function example
  7. javascript anonymous function call
  8. javascript anonymous function scope
  9. javascript anonymous function return object
  10. javascript anonymous function callback
  11. javascript anonymous function syntax
  12. javascript anonymous function async
  13. javascript anonymous function arrow syntax
  14. javascript anonymous function es6
  15. javascript anonymous function closure
  16. javascript anonymous function object
  17. javascript anonymous function as argument
  18. javascript anonymous function onclick
  19. javascript anonymous function use
  20. javascript anonymous function foreach

I'm simply trying to append a json created dynamically into an array that lives inside the state. The only way that I've found till now is to use concat, because push doesn't work. constructor() {...

I'm simply trying to append a json created dynamically into an array that lives inside the state. The only way that I've found till now is to use concat, because push doesn't work. constructor() {...

  1. push json reactjs