How to invoke javascript function from href for multiple links within a table -



How to invoke javascript function from href for multiple links within a table -

i have table contains column multiple filenames. filenames links of them pointing url , of them pointing local file. can download local file , point url in new window when clicked upon independently.

however, when seek dynamically passing in filename via js function within href, encounter uncaught syntaxerror: unexpected token :. not seem problem when phone call js function without parameter, however, links point lastly filename in column. can see @ bottom of browser javascript:openlink(filename) proper filename, error happens.

this code:

function searchajax(){ var $table = $("#tablegrid").tablesorter(), $tbody = $table.children("tbody"); $.ajax({ datatype: 'json', url: 'table_results', type: 'get', data: $('#filterform').serialize(), success: function(data) { $("#tablegrid").find("tr:gt(0)").remove(); $.each(data, function(index, result) { function openlink(value){ var link = value; if(link.indexof("http") > -1 ){ window.open(link, "_blank"); } else{ location.href="/projectv1?filename="+link; } }; $tbody.append( "<tr>" + "<td>" + result.regioncode + "</td>" + "<td>" + result.nationcode + "</td>" + "<td>" + '<a href="javascript:openlink('+result.link+')" target="_blank">link</a></td>"' + "</tr>"); }); $("#tablegrid").trigger("update"); } }); };

you can alter html appending code

$tbody.append( "<tr>" + "<td>" + result.regioncode + "</td>" + "<td>" + result.nationcode + "</td>" + "<td>" + '<a class="customlink" data-url="'+result.link+'">link</a></td>"' + "</tr>");

and can add together click event handler customlink .

$(this).data('url') retrieves link info html element's info attribute .

jquery(document).on( { click: function(e) { var url = $(this).data('url'); openlink(url); } } , '.customlink');

you should move openlink function definition outer scope

javascript href location-href

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -