2

I have main html page and then I need to add a dynamically created list of a template of another html file I created.

Basically I have a template for one item of the list in a separate HTML page.

So what I need to do is create instances of that html page, populate the correct data, and then add it to list of items on the main html page. I would know how to do this in java but I am not sure how to do this in HTML / Javasript.

What is the preferred way to dynamically create html object instances.

Thanks

user3470688
  • 539
  • 6
  • 21
  • To get the complete instance of the page in DOM using jquery, you can use "*" selector like $("*"); Now if you console.log($("*")); you will se complete html reference. Here you can modify your html and use it. If i understand your question correctly. – Mayank Nimje Dec 02 '16 at 14:41

1 Answers1

1

You can load the html template with jQuery load method

  $("#container").load('path/template.html', function(){
    // some code to populate your template      
  });

Note: this will only work through a web server, not locally as you will hit the browsers cross site origin policy. use apache or similar

Brad
  • 8,044
  • 10
  • 39
  • 50
  • How do I keep creating new instances of that template? That looks like it will load just that html page and I can modify. How do I create another one with different values? Also I would like to try to avoid any cross site issues, so is there a better way to try to do this? – user3470688 Dec 02 '16 at 14:56
  • just wrap it in a jquery.each loop http://api.jquery.com/jquery.each/ so for each instance load the template. The cross origin stuff is only if you are doing it on your local machine, once on a server its fine. Its baked into browsers. To be honest this is the only i know to do this without either a front end framwork (angular etc) or a backend (PHP, Ruby, C# etc). – Brad Dec 02 '16 at 15:01
  • I do have to do it on my local machine but we are using node.js – user3470688 Dec 02 '16 at 15:04
  • Ok, you can do it on your machine, you just need to load your web page through a webserver rather thant just opening it in a browser. apache can do this, its free and simple to use. Try something like xampp https://www.apachefriends.org/index.html – Brad Dec 02 '16 at 15:07
  • Or if you are on windows, just run it through IIS – Brad Dec 02 '16 at 15:07