Friday, January 8, 2016

how to avoid duplicate values in dropdownlist using jquery

<select>
    <option>HR</option>
    <option>Software</option>
    <option>Software</option>
    <option>Hardware</option>
    <option>Hardware</option>

    <select>
Javascript/ JQuery:

$(document).ready(function () {
    var usedNames = {};
    $("select > option").each(function () {
        if (usedNames[this.value]) {
            $(this).remove();
        } else {
            usedNames[this.value] = this.text;
        }
    });
});
//Another Method
$(document).ready(function () {
$(".select option").text(function(i,v){
  $(this).siblings("[text="+ v +"]").remove();
});
});

No comments:

Post a Comment