var valueArr = new Array(); if( $("input[name='type']").val() != ""){ valueArr = $("input[name='type']").val().split(","); } if(selectedValue != "" && $.inArray(selectedValue, valueArr) == -1){ valueArr.push(selectedValue); } $("input[name='type']").val(valueArr.sort().join());