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 have a dropdown lookupfield in newform.aspx sharepoint form...unlike choice dropdown the lookupdropdown is 'input' field and not 'select'.. so i raise an onchange event using jquery

$(document).ready(function() {
$("input[title='Names']").change(function()...

since sharepoint refers a lookup dropdown as a input field rather than select, if the index changes based on the values in the drop down box the change event does not fire..but i type in anything in the lookup dropdown box the event fires...what i m looking for is to fire the event when the values from the dropdown are selected..any suggestions?

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

First of all some notes about this (weired?) sharepoint behavior (when html select renders as input). If you have a lookup field in your new\edit forms, this lookup may renders differently depending on browser and (wow!) items count in lookup list. When in lookup list items count less than 20, than all ok, in every browser lookup field renders as html select:enter image description here
On the image above it is an ie and lookitup refers to a list with 19 items. When we added one more item in lookup list situation is changed:enter image description here
and in ie only, all other browsers render lookitup as select. This is actually input with image, with number of event attached to provide suggest/filtering. This suggest/filtering feature works only in ie.
Ok, how to capture change event. @James Montagne recently asked this question. SharePoint 2010 has a js function called _SetCtrlFromOpt. This function fires on new\edit forms every time when value of "suggested" input is changed. You can catch it by substitute with custom function:

ExecuteOrDelayUntilScriptLoaded(function(){
      var oldset = _SetCtrlFromOpt;
      _SetCtrlFromOpt = function(ctrl, opt){
            alert("changed!");
            oldset(ctrl, opt);
       }
},"core.js"); 

You need to add this js code to your new\edit form. Here ctrl is a DOM input element, and opt is an element that represents suggestion dropdown. opt.options[opt.selectedIndex].value returns lookup id of a selected item in source lookup list, and opt.options[opt.selectedIndex].text returns lookup value.

Note: _SetCtrlFromOpt handles all lookup fields on the new\edit form, so if you have more than one lookup on your forms, and if you want perform some action only on the one of them, you need to filter all others events, that will be fired.

share|improve this answer
    
Thanks kai it works!!!...but the alert pops twice once when i open the drop down and i have to double click on the option to select(once)..and then when the selected option enters inside the dropdownfield(twice)...can we restrict it to jus pop up once..Thanks again! –  spStacker Feb 2 '12 at 21:59
    
Try to remove alert line, it seems that pop up with alert fire lose focus event, which fire _SetCtrlFromOpt again :). After removing alert all should work as expected. –  Kai Feb 3 '12 at 9:04
    
yes it is working out, Thanks Kai! –  spStacker Feb 3 '12 at 16:12
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.