I cannot call a javascript function with a parameter in dynamically generated HTML code.
The same function is successfully called when I do not pass any parameter, but with the parameters that caused the function to be called. Not sure if this is a syntax error.
below is my code:
<!DOCTYPE html>
<html>
<body>
<p>Click the button to call a function with arguments</p>
<button onclick="myFunction('Harry Potter','Wizard')">Try it</button>
<script>
function myFunction(name,job) {
var name="prasad";
var str='<a href="#" onclick="javascript: fun('+name+')">link</a>';
document.write(str);
};
function fun(id1) {
alert("fun menthod "+id1);
}
</script>
</body>
</html>
If I do not pass the parameter, it will be called successfully.
source
share