Robbie Trencheny
Robbie Trencheny

Reputation: 559

String inside onclick

So heres my code

html += "<li class='status-"+peeps[i].online_presence+"' onclick='show_connect_message("+peeps[i].uid+");streamPublish("+peeps[i].uid+","+name+");'><a href='#' onclick='show_connect_message("+peeps[i].uid+");streamPublish("+peeps[i].uid+","+name+");return false;'>"+peeps[i].name+"</a></li>";

Basically, when inspecting the element I can see it is outputting a UID and name. Here is the line:

<li class="status-active" onclick="show_connect_message(503141088);streamPublish(503141088,David);"><a href="#" onclick="show_connect_message(503141088);streamPublish(503141088,David);return false;">David</a></li>

So what am I doing wrong. I need the name (David) in double quotes or single quotes because otherwise the console says that it can't find the function referenced.

Warning: Javascript Noob

Thanks for your help!

Upvotes: 2

Views: 1337

Answers (2)

Ash Burlaczenko
Ash Burlaczenko

Reputation: 25445

Try this

html += "<li class='status-"+peeps[i].online_presence+"' onclick='show_connect_message("+peeps[i].uid+");streamPublish("+peeps[i].uid+","""+name+""");'><a href='#' onclick='show_connect_message("+peeps[i].uid+");streamPublish("+peeps[i].uid+","""+name+""");return false;'>"+peeps[i].name+"</a></li>";

Upvotes: 0

Teneff
Teneff

Reputation: 32158

change

streamPublish("+peeps[i].uid+","+name+")

to

streamPublish("+peeps[i].uid+",\""+name+"\")

the backslash will escape the double quotes and will not break the string

Upvotes: 2

Related Questions