Search code examples
javascriptjquerydynamic-links

How to pass parameters in dynamic link for onClick function


Hey I am creating dynamic link as below. Here I am calling a function to go to the required page. I want to pass the parameters with function.

 $.each(data, function(key, value) {
        var str = value.split(':');
        trHTML += '<tr><td><a href="#" onClick="forward('+key+','+value+');">'+str[0]+'</a></td><td class="col-xs-4">'+str[1]+'</td><td class="col-xs-4">'+key+'</td></tr>';
        });

This is creating a link like this :

<a onclick="forward(241,vaibhavs:3847);" href="#">vaibhavs</a>

I know there is some problem with these parameters. They are going as variable names rather than values. The error as shown by Firebug is :

**SyntaxError: missing ) after argument list

     forward(241,vaibhavs:3847);**

So please guys suggest me the correct way to pass the parameters here.


Solution

  • Try providing quotes,

    <a onclick="forward('241','vaibhavs:3847');" href="#">vaibhavs</a>