0

I have a lookup column with more than 20 items and I want to retrieve the selected text in a variable.To achieve this I have tried the code below but I am getting an error(mentioned in the code).

Note : When i use an alert, it is showing the correct value.

$("[title='Unit']").blur(function()
{
 var sys = null;
 var unit = null;
 if(getField("select","Unit"))
 {

    sys = $("[title='Unit'] option:selected").text();
    unit = sys;
    sys += ' - ' + $("[title='System'] option:selected").text();
  }
  else if(getField("input","Unit"))
  {
     sys = getField("input","Unit");
     var opt=document.getElementById(sys.opt);
     **sys = opt.options[opt.selectedIndex].text;** // throws error Object does not support this action
     unit = sys;
     sys += ' - ' + $("[title='System'] option:selected").text();

   }


  });

2 Answers 2

0

To answer your questions i have created 2 list List1 has Country text column List2 has Country1 lookup columns which looks up Country Column from the List1

The lookup column is rendered as "select" when the items are less than 20 columns but it is rendered as "input" when the item are more than 20 items.

Below is the code you should paste in the content editor Webpart on the newform.aspx page. Update : I don't know how much efficient this approach is, its more like a work around for this situation.

alert(ctrl.id) gives of id of the current lookupdropdown field which was clicked, so based on that you can have a if loop checking for the id- so then it would know which specific drop down box was clicked.

       <script src="yourpath/jquery-1.7.2.js" type="text/javascript"></script>
            <script type="text/javascript">
            $(document).ready(function() {
            ExecuteOrDelayUntilScriptLoaded(function(){

                  var oldset = _SetCtrlFromOpt; 

                  _SetCtrlFromOpt = function(ctrl, opt){ 
       if(ctrl.id=="ctl00_m_g_aed3829a_4d8a_4426_9845_58723e0dddb7_ctl00_ctl05_ctl01_ctl00_ctl00_ctl04_ctl00_ctl01")
        {


                        var s = opt.options[opt.selectedIndex].text;

                       $("input[title='SelectedCountry1']").val(s);

                        oldset(ctrl, opt); 
                   } 

else
{
 oldset(ctrl, opt); 
}
        }

        if(ctrl.id="id of other look up dropdown box")
        {
        //
        }

            },"core.js"); 
            });</script>

The selected item from the drop down box gets displayed in the "SelectedCountry1" textbox. I have asked a similar kind of question months back here is the link Dropdown lookup column in Sharepoint list form (newform.aspx) You can refer to this link for excellent explanation.

7
  • Between i tried this approach as well and it is also working .. link
    – user9167
    Commented Jul 13, 2012 at 12:25
  • Actually in ur approach i am facing one issue , say i have three to four Lookup columns in same page which has more than 20 items, if i implement the code you shared, all the controls in the form are showing same value on change ... please help
    – user9167
    Commented Jul 13, 2012 at 12:51
  • Hi User9167- I have updated the answer.
    – SandeshR
    Commented Jul 13, 2012 at 18:17
  • Hi , I am getting error that 'ctrl' is undefined .. if i do this way
    – user9167
    Commented Jul 16, 2012 at 6:42
  • Hi User9167, sorry i am unable to check the code at my place, but i have updated it code, try it and see if it works for you
    – SandeshR
    Commented Jul 16, 2012 at 13:48
0

Why don't you use InfoPath? I guess it would be easier.

1
  • Thanks for your reply. But we want to implement this only with jquery/javascript
    – user9167
    Commented Jul 11, 2012 at 14:05

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.