You are viewing limited content. For full access, please sign in.

Question

Question

Forms - Why does setting a select field value using the URL set the selectedIndex to -1?

asked on March 22, 2021

We are using the URL to pre-populate a select field (Drop Down) on a form.

IE: ?Employee_Name=Test User

Then we are getting the selected index to see what is selected on document load

$(document).ready(function() {
  
  var dropDownListRef = document.getElementById('Field1');
 var selectedIndex = dropDownListRef.selectedIndex;

console.log(selectedIndex);

});

The selected index is always returning -1. Why is that?

0 0

Replies

replied on March 22, 2021

Are you matching to a drop-down that is populated on the Layout page or one that is populated from a Lookup?

If it is from a lookup, it is likely calling your code before the lookup has completed - you can try this instead: 

$(document).on('onloadlookupfinished', function (event) {
  
  var dropDownListRef = document.getElementById('Field1');
  var selectedIndex = dropDownListRef.selectedIndex;

  console.log(selectedIndex);

});

 

I tested both myself, and it worked with your code when I was using values populated on the Layout page, but I had to use my code with the values being populated by a lookup.

0 0
replied on March 22, 2021

It is a layout populated field. If I just open the form naturally it returns an index of 0, but if I pre-populate the field in the URL to any value 1, 2, 3 etc, it always returns -1. So strange.

0 0
replied on March 22, 2021

That is weird - because it did work for me.

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.