更改<a> download atrribute value trough javascript

时间:2017-03-14 23:01:44

标签: javascript jquery html web download

I am trying to change an <a> tag download attribute value with the input from a user.

The user should be able to enter the file name and then click download. I have searched all over and cannot seem to find the answer. Can you please give me some advice?

HTML:

 <input id="filename" type="text" onChange="inputChange(event)"/>

 <a download="RENAME-ME.csv" href="#" class="download">
       Download your file
 </a>

Javascript:

function inputChange(e) {
   console.log(document.getElementById("filename").value);
}

2 个答案:

答案 0 :(得分:0)

您可以像这样使用jQuery:

$('#filename').on('input', function() { 
   document.getElementsByClassName("download")[0].download = document.getElementById("filename").value
   console.log("File name:" + document.getElementsByClassName("download")[0].download )
});

您可以在JSFiddle

找到小提琴

答案 1 :(得分:0)

HTML:

updateProposals() {
  this._proposals = this._proposals.filter(...); // or whatever
  this.subject.next(Object.assign({}, this._proposals)); // emit completely new value
}

JAVASCRIPT:

<input id="filename" type="text" onChange="inputChange(event)"/>
<a download="RENAME-ME.csv" href="#" class="download" id="button">
   Download your file
</a>