Can you set the defaultSelected value of a select field programmaticlly with javascript. My problem is that when checking for values changed by the user I am getting true for the if condition below, when our developers programmaticlly change the selected option. I want them to also update the defaultSelected.
if (thisElement.options[j].selected !=
thisElement.options[j].defaultSelected)
{
dirty = true;
}
asked Nov 16, 2011 at 16:45
coder
6,25319 gold badges58 silver badges73 bronze badges
1 Answer 1
Whenever they make
select_element.value = 'someValue';
to select a value in drop down, make them also set the defaultSelected attribute of the option inside:
select_element.children[/*selected index*/].defaultSelected = true
Edit. In Jquery:
$('#select_element').val('someValue');
$('#select_element option:selected').attr('defaultSelected','true')
answered Nov 16, 2011 at 16:52
Arthur P
1,07010 silver badges16 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Vadym
Would you have to iterate over existing options and set the .defaultSelected to false in unselected option? This would apply to both multiselect and regular select.
lang-js