html - Call a JavaScript function with parameters when click on href -
i trying come code create href tag javascript functions gets parameters. parameters string , object converted json string. attempt this:
return '<a style="text-decoration:underline;cursor:pointer" target="_blank"' + ' href="javascript:gotostatenewwindow(\'trending\', \'' + json.stringify(params) + '\')">' + value + '</a>';
the error was:
uncaught syntaxerror: invalid or unexpected token
in inspect window looked this:
<a style="text-decoration:underline;cursor:pointer" target="_blank" href="javascript:gotostatenewwindow('trending', '{" projectid":2313,"alarmsonly":"true"}')"="">test</a>
can please explain doing wrong?
firstly, shouldn't <a>
element, <button>
. can use css make <a>
if want.
secondly, should rather create en element using document.createelement()
, add attributes , specify click event listener using addeventlistener()
method.
const element = document.createelement('button') element.setattribute('type', 'button') element.setattribute('style', 'text-decoration:underline;cursor:pointer;') element.setattribute('target', '_blank') element.addeventlistener('click', event => gotostatenewwindow('trending', params)) element.innerhtml = value return element.outerhtml
Comments
Post a Comment