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 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.

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

3 Answers 3

$("#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.

share|improve this answer
    
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 Mar 18 '13 at 14:28
up vote 2 down vote accepted

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();
share|improve this answer
    
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. –  Dave Paylor Mar 18 '13 at 20:46
    
@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 Mar 19 '13 at 0:11
    
Ah OK - thanks for clarifying. –  Dave Paylor Mar 19 '13 at 10:07

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

share|improve this answer

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.