jquery - Prevent onclick and retrieve parameters of function call

We've been asked to put in a band-aid measure to some code so this is going to look really dumb but bear with me.

We need to prevent an onclick method of an anchor and retrieve the parameters of the function call being made in order to call a different function with those parameters. Here is the code and html we have so far:

<p><a href="#" onclick="openMoreProductVideos('Title of video','VideoCode','false');return false;"><span class="txt">View all videos</span></a></p>

JS:

// undefine onclick so it is not handled with broken function
$('a[onClick^=openMoreProductVideos]').each(function () {
    this.onclick = undefined;   
});

    // grab parameters from onclick function call and submit to new function.
$('a[onClick^=openMoreProductVideos]').click(function () {
    strStart = "openMoreProductVideos(";
    strFinish = ");return false"
    srctext = $(this).parent().html();      
    var re = strStart + ".*?"+strFinish
    var newtext = srctext.match(re)[1];
    var callparams = newtext.substring(1,newtext.length-1);
    testparams(callparams);
  });

 // test function to see if parameters are working
 function testparams (a,b,c){
    console.log (a,b,c)
 }

Problem is that the returned string (callparams) is seen as one parameter. I am not a regex expert so I'm hoping that someone with more experience than I can quickly see the solution.

Many thanks.

Answers


if callparams is a string like "'Title of video','VideoCode','false'" then try

testparams(callparams.split(',')); 
   /* transforming string into an array 
    * ["'Title of video'", "'VideoCode'", "'false'"] 
    */
...

function testparams (pars){
   console.log (pars[0], pars[1], pars[2]);
   /* if necessary remove trailing quotes with .replace() method */
}

Need Your Help

FILE_FLAG_DELETE_ON_CLOSE and memory mapped files

c++ winapi memory-mapped-files

Not that it's particularly useful, but I'm curious as to why the following works, is it simply because the page still happens to be in memory even after the file is deleted? In which case, if the p...

How to have repeated items in an Ordered To-Many relationship?

ios core-data nsarray relationship

In Core Data of Xcode 4.3.2, an Ordered To-Many relationship is modelled with NSOrderedSet. It works well until I found the need to have repeated items in the relationship; it should really be mode...