Alma
Alma

Reputation: 4390

call a function by clicking on a dynamically-inserted link

I have a MVC view that has a link in jquery, by clicking this link it calls a JavaScript function. I am not sure how to call the function and pass a parameter to the function.

this is the link:

var cell5 = $('<td />').html('<a href="functioncall?FileName=' + 
e.Application_Filename + '&RemoteRefNumber=' + e.RemoteRefNumber + '&target="_blank">PDF</a>');

and this is the function I would like to call.

 function getpdf(FileName,RemoteRefNumber) {
     [...]
 }

Upvotes: 0

Views: 84

Answers (1)

scniro
scniro

Reputation: 16989

You can just wire this up inline via onclick, passing in your parameters. I assume you want to open a new browser window with some query params - observe the following...

 var cell5 = $('<td />').html('<a href="#" onclick="getpdf(\'' + e.Application_Filename + '\', \''  + e.RemoteRefNumber + '\');">PDF</a>');

window.getpdf = function(FileName, RemoteRefNumber) {

    // do you need to do anything else here?
    window.open('https://[...]?FileName=' + FileName + '&RemoteRefNumber=' + RemoteRefNumber);
}

Depending on what you're doing, why not just href it? Do you need to get into that function first? If not, this should suffice...

var cell5 = $('<td />').html('<a target="_blank" href="https://[...]?FileName=' + e.Application_Filename + '&RemoteRefNumber=' + e.RemoteRefNumber + '">PDF</a>')

note - in the first suggestion, I defined my function globally on window. Elsewise, that function is not available inline. See this JSFiddle for a demonstration between three different function declarations and the behavior

Upvotes: 2

Related Questions