I'm making a tagging system, and I cannot figure out how to prevent users from spamming the same tag. My only thought is to add each into an array. And make it not add it again once they click it again, but I cannot figure it out. If anyone could help that would be great!
here is my code sample:
<select multiple="multiple" size="10" id="tag_name">
<option>apple</option>
<option>pear</option>
<option>pineapple</option>
<option>peach</option>
<option>strawberry</option>
<option>raspberry</option>
<option>cherry</option>
<option>watermelon</option>
<option>orange</option>
<option>banana</option>
<option>grape</option>
<option>honeydew</option>
<option>apricot</option>
</select>
<!-- appended tags wrapper -->
<div class="tags"></div>
<script type="text/javascript">
var tag_count = 0;
var tag_array = "";
$("#tag_name").click(function(){
var tag = $("#tag_name").val();
if(!tag == ""){
tag_array = tag_array+tag+", ";
tag_count = tag_count+1;
if(????){
if(tag_count != 11){
$(".tags").append("<div style=\"margin: 5px;\" class=\"tag\"><p>"+tag+"</p><a href=\"#\">x</a></div>");
} else {
$(".tags").append("<div style=\"margin: 5px;\" class=\"tag\"><p>15 tags already selected!</p><a href=\"#\">x</a></div>");
$("#tag_name").attr("disabled","disabled");
}
}
}
});
</script>
if(????){? 2) Every time you will reload the page, all the tag will be lost or you registered them in database or somewhere? 3) Every time you add a tag, you create a new div with the tag as content, right? Just make a select with all.tagelement and check if you already same tag maybesetwould be an approach, which is similar to an array, but does only allow one value once. developer.mozilla.org/de/docs/Web/JavaScript/Reference/…