0

I am trying to set the download attribute on a link tag using jquery. In general I am using

<a type="button" id="pdf_modal_button" class="btn btn-primary" href="/download/x.pdf" download target="_blank">Download</a>

which downloads the pdf fine.

The problem arises when I generate a link:

<a type="button" id="pdf_modal_button" class="btn btn-primary" href=/download/>Download</a>

and then try to update the link with jquery:

    var _href = $('#pdf_modal_button').attr("href");
    $('#pdf_modal_button').attr("href", _href + '/x.pdf')
    $('#pdf_modal_button').attr("download", true)
    $('#pdf_modal_button').attr("target", "_blank")

it alters the link to download="true"

<a type="button" id="pdf_modal_button" class="btn btn-primary" href="/download/x.pdf" download="true" target="_blank">Download</a>

This causes the browser to open the pdf in a new tab and displays it, instead of just downloading.

Question is, how do I get it to just have a download attribute, and not download="true"

Setting the url in the download attribute doesn't work either.

devops to dev
  • 31
  • 1
  • 7

2 Answers2

0

You're setting it to true when you call

$('#pdf_modal_button').attr("download", true)

Try replacing true with the download link

$('#pdf_modal_button').attr("download", "x.pdf")

0

It seems like an empty string is what I wanted, figured it out from copying the element through chromium.

var _href = $('#pdf_modal_button').attr("href");
$('#pdf_modal_button').attr("href", _href + '/x.pdf')
$('#pdf_modal_button').attr("download", "")
$('#pdf_modal_button').attr("target", "_blank")

generates

<a type="button" id="pdf_modal_button" class="btn btn-primary" href="/download/x.pdf" download target="_blank">Download</a>
devops to dev
  • 31
  • 1
  • 7