Onclick = "doSomething ([object Object])" Uncaught SyntaxError: Unexpected identifier
4 answers
The reason is that when you use string concatenation, params is called into a string, as a result you get something like [object Object] in brackets.
You better put the parameters like var params = '{a:1,b:2}'; .
Upd.
As suggested in the comments, another viable approach uses JSON.stringify :
var params = {a:1,b:2}; var str = '<a href="#" onclick="doSomething(' + JSON.stringify(params) + ')">aaaa</a>'; document.write(str); Please note that JSON.stringify may not be supported by older browsers, and you will need to include additional libraries to make them work.
+10