1

I feel like this should be an easy one but am having trouble finding the answer here and via Google. Using jQuery, how do I retrieve the currently selected value from a dropdown in an EditForm/NewForm. The dropdown is a lookup column?

I've tried:

 $('#my_lookup_input_id').val()

Which returns a static value; if the drop down is changed, it won't alter the val() output.

I found this answer but I am using SP2007. I need to be able to get the lookup ID/name pair from the dropdown in order to update another record from a different list using SPServices and GetListItems.

4
  • You'd need an onchange or blur function if you need to check the value again. Commented Mar 15, 2013 at 20:47
  • Is it rendering as an input tag or a select tag? Commented Mar 15, 2013 at 20:59
  • Have you tried $('#my_lookup_input_id :selected').val() ?
    – James Love
    Commented Mar 15, 2013 at 21:01
  • @andrew.petersen15 - it is rendered as an input tag.
    – thornomad
    Commented Mar 18, 2013 at 14:27

4 Answers 4

3

Use the F12 tools. Depending how many values are in your Lookup Field SharePoint will render it differently. I do believe the thershold is 20 items.

Under 20 the Lookup will render as a Select field. ex $("select[title='"+ fieldTitle +"']").val();

Over 20 ex $("input[title='"+ fieldTitle +"']").val();

if you want to get the ids of the input there is a attribute "optHid". Here is samples to get all values and ids. choices = $("input[title='" +fieldTitle +"']").attr("choices"); hiddenInput = $("input[title='" +fieldTitle +"']").attr("optHid");

2
$("#my_lookup_input_id option:selected").val()

This should get the selected value but as indicated by PirateEric you would need to call this again if the value changes.

1
  • So - that doesn't work for me. If I do $("#my_lookup_input_id").val() I get the original value but if I do $("#my_lookup_input_id option:selected").val() I get undefined. (same for just :selected).
    – thornomad
    Commented Mar 18, 2013 at 14:28
2

So, after some more digging this morning and banging my head I found a solution using SPServices. First, convert the field from a complex to a simple drop down:

$().SPServices.SPComplexToSimpleDropdown({  
    columnName: "myColumn"
});

Then, you can get the ID (and then the value) by using what SPServices has cleverly injected:

$('#SPComplexToSimpleDropdown_myColumn').val();

I'm still not sure of a way to do this without SPServices but I think it has to do with how SharePoint is doing funny business with the input field.

PS: set the value

If you want to set the value of the dropdown you need to fire the change() event after setting it so the internal code gets tapped.

$('#SPComplexToSimpleDropdown_myColumn').val(15).change();
3
  • Glad you are sorted but there is definitely something odd going on because I have just tested a standard 2010 create and edit form with a lookup column and they are rendered as select tags. Commented Mar 18, 2013 at 20:46
  • 1
    @DavePaylor - I am using 2007, maybe that's the difference? It generates a strange input field with lookups of more than 20 items that has a hidden input field that manages the value changes via javascript. Or something.
    – thornomad
    Commented Mar 19, 2013 at 0:11
  • i am having same problem, i am using SharePoint 2010. above script not working for me Commented Apr 29, 2016 at 10:51
-1
$("select[title='Lookup field Title'] :selected").text()

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.