getOption always return null

bourhoud
Kilo Expert

Hi all,

I have a problem for getting value of a choice list, in a client script.

In a form, I have a field, type choice, with any specified choices. Its name is u_quotation_type.

In a client script, I try to get dynamically the display value of an option.

For this, I'm trying to use g_form.getOption, and then get its innerText property.

And there, is the problem : my getOption always return null..

Below my (simple) code :

alert( g_form.getValue('u_quotation_type'));     // Displays "option"

alert(g_form.getOption("u_quotation_type", 'option')); // Displays null

alert(g_form.getOption("u_quotation_type", 'Option')); // Displays null

alert(g_form.getOption("u_quotation_type", g_form.getValue('u_quotation_type'))); // Displays null

I'm working on a Helsinki instance.

Can anyone give me help, please ?

18 REPLIES 18

The above method also gives you the value of the choice list. did you try it? if not once try it and see if it is what you wanted because it pops up an alert box showing the value of the choice list and if you want to add the choice list name in it too you can do this



var a = g_form.getValue('u_quotation_type');


      alert("Quotation type is:     " +a);



Thank you


Yes, I've already tried it, as I said in my first post :



alert( g_form.getValue('u_quotation_type'));     // Displays "option"


alert(g_form.getOption("u_quotation_type", 'option')); // Displays null




(Thanks again for your help)


In that case try this and see alert(g_form.getOption("u_quotation_type", 'option').text); this should work



Thank you


harishdasari
Tera Guru

Hi Pierre,



usually getOption will be used in on change client scripts,



example :


if(g_form.getOption('priority', '1');


{


alert('you have selected high priority');


}


else


{


alert('you have selected priority as high' + g_form.getValue('priority'));


}



as per servicenow wiki


syntax: getOption(fieldName, choiceValue)


Returns the <option> element for a select box named fieldName and where choiceValue matches the option value.
Returns null if the field is not found or the option is not found.
Parameters:
fieldName - specifies the name of the field.
choiceValue - specifies the value of the option to return.
Returns:
HTMLElement - the specified HTML element.

I tried your example, but no positive result.



I changed the value I want, to "1", and still null.



But, what do you mean by "usually getOption will be used in on change client scripts" ? I use it in an onload client script, not an onchange one. Could it be the reason ?