2

I have a link for downloading Audio Files. I wish to keep it hidden and allow the user to call it using a button. But somehow I am unable to trigger the click event.

Here is HTML:

<a class="APopupDown" data-icon="home" id="DownloadFile" href="http://yahoo.com/abc.mp3" download="My File Name">Download File</a>

<button data-icon="home" href="#" onclick="DlMsg()">Download</button>

And here is the trigger.

function DlMsg()
{
    alert("I am here");
    $('#DownloadFile').trigger('click');

}

A Jfiddle example is here

As you can see the diredct link works but the button doesn't. Any idea what I am doing wrong?

Thanks

----- UPDATE -------

I have made the change as per Jai's suggestion. However, it now starts playing the Audio instead of downloading, whereas what I want is to use HTML5's download functionality. Here is the jsfiddle link

2 Answers 2

1

Got the answer. Just see this fiddle http://jsfiddle.net/EWQ6n/512/

Source:

<a class="APopupDown" class="test" data-icon="home" id="DownloadFile" href="http://www.jplayer.org/audio/mp3/TSP-01-Cro_magnon_man.mp3" download="My File Name">Download File</a>

<button data-icon="home" id="btnDownload">Download</button>

Script:

$("#btnDownload").click(function(e){
    $("#DownloadFile").get(0).click();
});

I just used like this

$("#DownloadFile").get(0).click();

to trigger the anchor click event.

Sign up to request clarification or add additional context in comments.

1 Comment

Working is JSfiddle but not in the application. I wonder why.
0

You need to have a click event bound to that element first:

$('#DownloadFile').click(function(e){
    e.preventDefault();
    window.location.href = this.href;
});

then your trigger will work:

$('#DownloadFile').trigger('click');

Demo @ Fiddle 506

1 Comment

It doesn't download the file. It starts playing it within the browser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.