Created
January 29, 2010 11:22
-
-
Save mudge/289659 to your computer and use it in GitHub Desktop.
Provide a list of commonly-used tags to populate a text field.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Provide links to commonly-used tags that can be clicked by a user to | |
* populate a text field with comma-separated tags (ensuring that no | |
* duplicates are entered). | |
* | |
* This assumes that the text field has an ID of `tag_string` and the links | |
* have the class `tag`, e.g. | |
* | |
* <input type="text" name="tag_string" id="tag_string" /> | |
* <ul> | |
* <li><a href="#" class="tag">blue</a></li> | |
* <li><a href="#" class="tag">red</a></li> | |
* </ul> | |
*/ | |
$('a.tag').click(function() { | |
var current_tag_string = $('#tag_string').val(); | |
var current_tags = current_tag_string.split(/\s*,\s*/); | |
var new_tag = $(this).text(); | |
if (-1 == $.inArray(new_tag, current_tags)) { | |
if ('' == current_tag_string) { | |
$('#tag_string').val(new_tag); | |
} else { | |
$('#tag_string').val(current_tag_string + ', ' + new_tag); | |
} | |
} | |
return false; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment