Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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.click();
    return false;
  });
  $(this).prepend($btn);
});​
share|improve this answer
    
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 Sep 25 '12 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 Sep 25 '12 at 17:51
    
awesome, thanks for your help! –  hrezs Sep 25 '12 at 18:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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