Onclick with HTML

The following does not seem to work:
var rowHTML = '<td>' + '<a href="javascript:void(0)" onclick="highlightSpot(' + spot.name + ');">' + spot.name + '</a>' + '</td>' + '<td>'+distance+'</td>';

	var table = document.getElementById(tableID);
  	rowElement = document.createElement('tr');            
 	newRow = table.appendChild(rowElement);
	newRow.innerHTML = rowHTML;

Open in new window


The function highlightSpot() is not being called.
However, when I use onclick="highlightSpot()", the function is called.

Please let me know what the problem could be. Thanks!
dshrenikAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
sammySeltzerConnect With a Mentor Commented:
Well, you can try escapting the quotes like:

onclick='highlightSpot("""+ spot.name +""");'>
0
 
sammySeltzerCommented:
You don't show the function!

Could it be that the function doesn't have a param?

More like function highlightSpot()
{
...
...
}
0
 
dshrenikAuthor Commented:
There's no problem wth the function. It works fine.

I narrowed down on the problem. When I pass a numeric parameter like "12" through onclick="highlightSpot(' + spot.name + ');", it works fine.

However, it does not work when I pass a text parameter like "ABC".
0
 
dshrenikAuthor Commented:
I have an alert command as the first line of the function.

The alert works fine with numeric parameters but not otherwise.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.