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 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();

   }


  });
share|improve this question
add comment

2 Answers 2

up vote 0 down vote accepted

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 lookupcoloumn in sharepoint list form (newform.aspx)2010 You can refer to this link for excellent explanation.

share|improve this answer
    
Between i tried this approach as well and it is also working .. link –  user9167 Jul 13 '12 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 Jul 13 '12 at 12:51
    
Hi User9167- I have updated the answer. –  spStacker Jul 13 '12 at 18:17
    
Hi , I am getting error that 'ctrl' is undefined .. if i do this way –  user9167 Jul 16 '12 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 –  spStacker Jul 16 '12 at 13:48
show 2 more comments

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

share|improve this answer
    
Thanks for your reply. But we want to implement this only with jquery/javascript –  user9167 Jul 11 '12 at 14:05
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.