Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript pass variable in href onclick request

I know this is really basic javascript but for some reason, I can't seem to get my link's onclick function to work when passing a parameter.

I have tried escaping the quotes, adding different types of quotes and adding the raw variable as a string.

I have it working with the below but it says that "XYZ is undefined"

function renderLink(value, meta, record)
        {
            var type = record.data['name']; //value is XYZ  


            return '<a href="javascript:void(0);" onclick="getReport('+type+'); return false;"></a>';
        }

function getReport(type){

    alert(type);
}
like image 293
pm13 Avatar asked Oct 21 '11 13:10

pm13


1 Answers

return '<a href="javascript:void(0);" onclick="getReport('+type+'); return false;"></a>';

You need to escape the string:

return '<a href="javascript:void(0);" onclick="getReport(\''+type+'\'); return false;"></a>';
like image 94
Joe Avatar answered Oct 22 '22 04:10

Joe