Wednesday, 4 September 2013

Call to Javascript function with in dynamically created HTML tag not working

Call to Javascript function with in dynamically created HTML tag not working

This seemed to be a simple javascript but I feel so dumb to ask this
question.. but I don't know why this is not working. I have a method that
dynamically creates some hyperlinks on the page. The hyperlinks are
suppose to call an executable with a url as parameter. So, I use href with
a call to this function that has the code to call the executable.
Non-dynamic anchor tag code works just fine to launch an exe.
But, I need this to work with dynamic hyperlinks. I have tried to simplify
the problem and created the following jsFiddle to demonstrate the issue.
The link doesn't call my method. Am I missing something stupid? I want
this to work in IE.
I already tried some solutions posted by some people saying: I should try
href="#" onclick="myfunc();". Somehow that didn't work either.
Here is my code:
function myfunc(url){
alert(url);
};
function dynfunc(){
var value = "This is a link";
var url="http://www.google.com";
var table = $("<table id='tab1'></table>");
var row = $("<tr></tr>");
var valueColumn = $("<td></td>");
$('<a href="javascript: myfunc(\'' + url +
'\');"></a>').html(value).appendTo(valueColumn);
valueColumn.appendTo(row);
row.appendTo(table);
$("#div1").append(table);
};
dynfunc();

No comments:

Post a Comment