I have a dropdown box, It has no values in them. I have to set it preselected with a value available in a variable. This is what I have done but it does not set this value.
if(condition)
{
var selectedVal = "myValue";
$("#myDropDownId").val(selectedVal);
}
as per info from google, the above one works when there are options already available in the dropdown.
I tried with the below as well
if(condition)
{
var selectedVal = "myValue";
$("#myDropDownId option:selected").val(selectedVal);
}
What is the correct syntax for this please?
optionelements inside theselect? If that's the case, you probably want to add one with a value/text matching the value of your variable, rather than trying to set the select value.