Search code examples
jqueryinsertafter

JQuery Syntax error, unrecognized expression: ,


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.


Solution

  • $(', ') 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 + ', ';
    })