7c7 < $('span.suggestedterm').each ( function() { --- > $('label.suggestedterm_keyword, label.suggestedterm_keyword_selected').each ( function() { 11a12 > 13,20c14,48 < var input = $(this).parent().siblings('input'); < var text = $(this).text(); < if (((', ' + input.val() + ',').indexOf(', ' + text + ',') < 0) && ((', ' + input.val() + ',').indexOf(', "' + text + '",') < 0)) { < if ((input.val()).length > 0) { < input.val(input.val() + ', '); < } < input.val(input.val() + text); < } --- > var tags = $(this).parent().siblings('input'); > var text = cleanKeyword($(this).text()); > var current = $.trim(tags.val()); > var regexp = keywordRegexp(text); > > if($(this).is('.suggestedterm_keyword_selected')) { > $(this).removeClass('suggestedterm_keyword_selected'); > $(this).addClass('suggestedterm_keyword'); > if(regexp.test(current)) { > > current = current.replace(regexp, '$1$2'); > > // Deal with a remaining extra comma > current = current.replace(/^\s*,/, ''); > current = current.replace(/,\s*$/, ''); > current = current.replace(/,\s*,/, ','); > tags.val(current); > } > } else { > > > $(this).removeClass('suggestedterm_keyword'); > $(this).addClass('suggestedterm_keyword_selected'); > > if(!regexp.test(current)) { > if(current == '') { > tags.val(text); > } > else{ > tags.val(current + ',' + text); > } > } > > > } 24a53,67 > > function keywordRegexp(keyword) { > return new RegExp('(^|,|, )\s*' + keyword + '\s*(,|$)'); > } > > /** > * Perform any necessary functions to cleanup the keyword > */ > function cleanKeyword(keyword) { > // If it has a comma IN it, surround with quotes > if(keyword.indexOf(',') != -1) { > keyword = '"' + keyword + '"' > } > return keyword; > }