3

I have 5 values in my lookup column. I need to select one and show it there by default.

document.getElementById("IdofLookupField").value="Country";

But this is not working, this doesn't set the value of lookup field as Country. Country is already a value in that lookup column.

4

2 Answers 2

3

Adding some code as you asked in comment.

<option value="2">Country< /option>

You need the value of the option which has Country in innerHTML.

Get all options by title attribute instead of id as id always generates dynamically.

var allOptions = document.querySelectorAll("[title=Category] option");

Now find Country and set as selected.

var allOptions = document.querySelectorAll("[title=Category] option");

[].some.call(allOptions, function(option) {
    if (option.innerHTML == "Country") {
        option.selected = "selected";
        return true;
    }
});

PS: Assuming Category is the name of the lookup column. Need to change in your case.

2

For lookup select options, the value of each option is the Id of the lookup list item, not the display column value.

<option value="2">Country<option>

Here, 2 is the Id of the lookup list item that has value Country for the lookup display column.

You can use the Id if you want to select by value, or you can try selecting by the display value of the option.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.