coder
coder

Reputation: 6233

Can you set the defaultSelected value of a select element with javascript

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;
        }

Upvotes: 2

Views: 2943

Answers (1)

Arthur P
Arthur P

Reputation: 1060

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')

Upvotes: 2

Related Questions