2

I am trying to add a button for my users to clear a multiselect lookup value using JQuery or Javascript.

I have found some information about setting a default value on a single select lookup, but not the opposite.

I believe I can get the field using get_item("field name"). Is there a built in method for removing selected items?

Thanks, Patrick

1 Answer 1

4

Clear it? Would it be good enough to just move the items back to where they came from, by selecting them on the right side and clicking the button.

Something like this:

$('.ms-formbody').filter(function() {
  return $(this).html().match(/spfieldlookup/i) && $(this).find('select').size() >= 1;
}).each(function() {
  var $btn = $('<button>').text('Clear');
  var $select = $(this).find('select:last');
  var $remove = $(this).find('button:last');
  $btn.click(function() {
    $select.val($select.find('option').map(function() {
      return $(this).val();
    }).toArray());
    $remove.removeAttr('disabled').click();
    return false;
  });
  $(this).prepend($btn);
});​
3
  • thanks for the reply, I'm trying to understand how this works. So you collect all lookups in an array, reference the button on the page that has the text 'Clear', then for each field, it selects all the selected options, then removes them? Is that correct?
    – hrezs
    Commented Sep 25, 2012 at 17:14
  • Yes. Essentials by lines: 1-3: select all SPFieldLookupMultis (not all are called multi actually, so you need the size-check of 'select'. 4: Create your clear-button. 5: right-side select. 6: the "< Remove"-button. 7: When your button is clicked. 8-10: Choose all possible options in the right-side select. 11: Click the "< Remove"-button. 12: Prevent propagation. 14: Add your clear-button to .ms-formbody
    – eirikb
    Commented Sep 25, 2012 at 17:51
  • before $remove.click(); must to remove disabled attribute to enable click. replace with: $remove.removeAttr('disabled').click()
    – WantToDo
    Commented Mar 17, 2021 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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