Passing a string in onclick function's attribute of element dynamically created element
You have to use proper string sytax. This
"<a href='#' onclick='test('"+elem[i].url+"')'>dd</a><br><br>"
will result in
<a href='#' onclick='test('http://domain.tld')'>dd</a><br><br>
You cannot use ' for onclick and the parameters of test. Use \" instead.
"<a href='#' onclick='test(\""+elem[i].url+"\")'>dd</a><br><br>"
Which results in
<a href='#' onclick='test("http://domain.tld")'>dd</a><br><br>
Your problem is, that str+="<a href='#' onclick='test('"+elem[i].url+"')'>dd</a><br/><br/>";
will return a string like "<a href='#' onclick='test('your_url')'>dd</a><br/><br/>"
. This will generate a html like this:
<a href='#' onclick='test('your_url')'>dd</a><br/><br/>
In this case, the onclick attribute contains only 'test('.
Try this:
str+="<a href='#' onclick='test(\""+elem[i].url+"\")'>dd</a><br/><br/>";
This will generate a html like this:
<a href='#' onclick='test("your_url")'>dd</a><br/><br/>