TonyW
TonyW

Reputation: 18875

Get tags value in "tag box" created in jQuery

I came across this post on SO, and I basically copied the code (html, css and jquery) to my web page. Everything works except that I don't know how to get the tags when my html form is submitted.

From my understanding of the javascript code, the tags are stored in tags, they are not stored in fields, so my question is how to capture those tags when the form where it's embedded is submitted?

Thanks

Upvotes: 0

Views: 406

Answers (2)

merlin
merlin

Reputation: 34

You can iterate a jquery object and get the child's content in this way:

var tags = '';
$('#tags > span').each(function() {
    tags = tags + $(this).html() + ',';
});
$('#inputInForm').val(tags);

Upvotes: 1

christian314159
christian314159

Reputation: 639

Just add a hidden input field to each of your tags like this when you create them:

<span class="tag">
    tag-name
    <input type="hidden" name="tags[]" value="tag-name">
</span>

This way you will automatically get the tags array when you post the form (obviously those tags should be within your form).

Upvotes: 1

Related Questions