2

I have a lookup field in the list and it is has duplicate values.

I found the below code and tried to use it.

<script type="text/javascript" src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
$( document ).ready(function() {
var usedNames = {};
$("select[name='Team'] > option").each(function () {
    if(usedNames[this.text]) {
        $(this).remove();
    } else {
        usedNames[this.text] = this.value;
    }
});

});
</script>

But this doesn't work, what extra things do I need to do?

  • Show some sample input/output. or screen shots – Atish Dipongkor - MVP Nov 19 '15 at 12:46
  • 2
    It would be best to use the enforce unique values setting on your lookup column in your list so you don't get duplicates. This code is only going to "fix" it on the display, not resolve your actual problems. – Eric Alexander Nov 19 '15 at 12:49
  • I want to do it for EditForm.aspx page...and also applying enforce unique values doesnt work – KumarV Nov 19 '15 at 12:52
  • select[name='Team'] is that correct? check it inspecting DOM from browser – Atish Dipongkor - MVP Nov 19 '15 at 12:57
  • If you enable force unique values, you then have to go back and clean up the duplicate items. You will have a clean list, no duplicates, and no more future duplicates. – Eric Alexander Nov 19 '15 at 13:08
4

Your code worked for me I have just changed the selector.

$("select[name='Team'] > option")

Instead of above

$("select[title='Name of your column'] > option")

I have replaced name with title as I did not find it in the DOM

enter image description here

  • 2
    While it works, it doesn't fix your underlying problem though – Eric Alexander Nov 19 '15 at 13:27
  • I agree. I have just tried to solve his js problem. OP should follow your suggestion for best practice @EricAlexander – Atish Dipongkor - MVP Nov 19 '15 at 13:30
  • Has anyone asked OP if the underlying List can be set to No Duplicates? I have come accross multiple Master-Detail use cases where we could not and needed the same remove-duplicates functionality. – Danny '365CSI' Engelman Nov 20 '15 at 8:23
  • It can be I guess as OP has said applying enforce unique values doesnt work in one of his comment. @DannyEngelman – Atish Dipongkor - MVP Nov 20 '15 at 8:28
  • My assumption by "it doesn't work" was that he didn't clean up the list data thinking that would solve the problem, when it wouldn't. – Eric Alexander Nov 20 '15 at 12:54
4

Your underlying problem is your relational list allows duplicate values to be inserted and gives you duplicate values when used in your forms. The fix, and not the bandaid solution, is to enable the enforce unique values on the lookup field in the lookup list and delete the duplicate entries.

1

You do not want to remove the Selected value from your dropdown

and do not need jQuery

console.clear();
var doubles=[];
var select=document.querySelectorAll("select[id^=Name]")[0];
console.log(select.childNodes);//must return a nodeList
[].slice.call(select.childNodes).forEach(function(el,n){//convert nodeList to array
    console.log(n,el,el.selected);
    var v=el.innerHTML;
    if(el.selected) {
        doubles.push(v);
    } else {
        if(doubles.indexOf(v)!==-1){
            select.removeChild(el);
        }else{
            doubles.push(v)
        }
    }
    });
console.log(doubles);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.