2

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

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.