Here is my HTML code,
<ul>
<li> Download <a href="#">file1</a> </li>
<li> Download <a href="#">file2</a> </li>
<li> Download <a href="#">file3</a> </li>
</ul>
I want to create something like "href=filename.txt"
. The "filename"
of the text file and its content needs to be created dynamically based on which tag was clicked.
I use a web API to get data in JSON format and hence believe security should not be an issue.
The reason to work jquery with static content is they are known to DOM, and can handle the events of known elements. but with dynamic contents you have to bind the event with that element by using .
Create Dynamic Controlsappend() event with the control in which you want to add some dynamic controls and in that append event you need to write the HTML code similar to what you might have written in the body section and this will work for you, for example: <script> $(document). ready(function () {
In this example, we should go with the following steps to implement dynamic data loading using jQuery. Create UI to be appended for each user information. Download and import latest version of jQuery library files and jQuery UI files. Create custom jQuery handlers to append UI and load dynamic data into it.
The jQuery load() method is a simple, but powerful AJAX method. The load() method loads data from a server and puts the returned data into the selected element.
As of HTML5, you can use a combination of data:
URL and download
attribute. For example,
<a href="data:text/plain;charset=UTF-8,Hello%20World!" download="filename.txt">Download</a>
Or, programatically,
<a id="programatically" href="#" download="date.txt">Download</a>
$("a#programatically").click(function() {
var now = new Date().toString();
this.href = "data:text/plain;charset=UTF-8," + encodeURIComponent(now);
});
See it here.
Unfortunately, download
attribute is not fully supported and data:
URLs are in good track.
Now, for a better cross-browser support you will need to create the file dynamically at server-side.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With