2

I'm using jQuery tags input library in my angularJS project. On frontend side, I able to add the tags, but when I retrieve the texts for that input. It contains spaces in between the values. I tried to use replace function on the string variable, it doesn't work.

HTML

<input id="news_tags" data-js-tags-input="" type="text" name="example-tags1" value="" ng-model="news.tags" 
class="form-control ng-pristine ng-untouched ng-valid ng-empty" data-tagsinput-init="true" style="display: none;">

<div id="news_tags_tagsinput" class="tagsinput" style="width: 100%; min-height: 36px; height: 36px;">
  <span class="tag">
    <span>tag1&nbsp;&nbsp;</span>
    <a href="#" title="Removing tag">x</a>
  </span>
  <span class="tag">
    <span>tag2&nbsp;&nbsp;</span>
    <a href="#" title="Removing tag">x</a>
  </span>
  <div id="news_tags_addTag">
    <input id="news_tags_tag" value="" data-default="Add tag" style="color: rgb(102, 102, 102); width: 68px;">
  </div>
  <div class="tags_clear"></div>
</div>

Controller

var tags  = jQuery("#news_tags_tagsinput .tag span").text().trim();
alert((typeof tags)); // String
alert(tags.replace(" ", ",")); // tag1 tag2
alert(tags.replace(" ", ",")); // tag1 tag2
alert(tags.replace("&nbsp;", ",")); // tag1 tag2
alert(tags.replace("&nbsp;&nbsp;", ",")); // tag1 tag2

Output

tag1 tag2

Instead Of

tag1,tag2

4 Answers 4

1

Change to pattern split string

var tags  = jQuery("#news_tags_tagsinput .tag span").text().trim();

alert(tags.split(/\s+/));

the regex means

\s = find whitespace character
+ = one or more characters
Sign up to request clarification or add additional context in comments.

Comments

1

The jQuery plugin handles this for you. Instead of trying to get the tag spans manually, you should simply fetch the value of the underlying input:

jQuery("#news_tags").val() //comma-delimited list of tags

2 Comments

This will returns empty in my case because my input value is empty, and the plugin library only insert new div tag class everytime user inserts new tag.
I looked at the plugin source. There is a function named updateTagsField which is called every time the user adds a tag. Even if your input is initialized with an empty value, calling .val() on it will return a comma-delimited list of the tags a user has added to it.
1
var tags = jQuery("#news_tags_tagsinput .tag span").slice();
alert(tags[0].textContent.trim() + "," + tags[1].textContent.trim());

https://jsfiddle.net/j4d0ko17/

Comments

0

For angular js I recommend us angular library instead jquery. You can try with http://mbenford.github.io/ngTagsInput/

var tags  = jQuery("#news_tags_tagsinput .tag span").text().trim();
alert((tags.split(/\s+/)).join());

Though, I do not recommended split using space as any of tag contains space, will be considered as multiple tag instead single.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.