JQuery Syntax error, unrecognized expression: ,

panzhuli picture panzhuli · Jul 31, 2012 · Viewed 8.2k times · Source

I'm trying to simply add a comma after each element in a list.

$('#tag_cloud-2 div.tagcloud a').each(function(){
  console.log($(this));
  $(", ").after($(this));
});

The console spits out the tags so I know it is finding them. I've tried insertAfter as well, but with no luck. This seams like it should be such a simple thing to do!! Thanks for pointing out what I'm missing.

Answer

thecodeparadox picture thecodeparadox · Jul 31, 2012

$(', ') is treated as selector(invalid), because it can't create text node with containing string.

But

$("<span>, </span>").after($(this)); will work because of a valid markup.

Try:

$(this).after(', ');

OR

$(this).append(', ');

OR

$(this).text(function(i, oldText) {
  return oldText + ', ';
})