65

Hi can somebody tell me what is the wrong in the below code?

Selected="selected" not working for me.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <select id="tstselect" name="tstselect" onchange="showselected(this.value);">
    <option value="0" >0</option>
    <option value="1" >1</option>
    <option value="2" >2</option>
    <option value="3" selected="selected">3</option>
    </select>
jestges
  • 3,686
  • 24
  • 59
  • 95
  • 1
    Worked for me with no issues. value 3 was the pre-selected item on my page – ScottJShea Feb 24 '12 at 06:47
  • Looks fine to me. Works fine in JSbin: http://jsbin.com/awodej you must have something else going on in the page. – DA. Feb 24 '12 at 06:47
  • 1
    works fine: http://jsfiddle.net/88Hfk/ and seriously, HTML4 transitional? move on to html5! – Joseph Feb 24 '12 at 06:49
  • I've onchange event in my code like onchange="showselected(this.value);" is there any impact? – jestges Feb 24 '12 at 06:49
  • better type in everything. you are not helping yourself by hiding stuff. – Joseph Feb 24 '12 at 06:50
  • seems fine to me. perhaps you have some random javascript deselecting combo boxes or something of that nature. – Robert Van Sant Feb 24 '12 at 06:52
  • 1
    its working in all browser. can we look into all your html/javascript so that we can see if there is anything affecting it.. – Bert Feb 24 '12 at 06:53
  • When I copy the source and copy into blank html page, it is working fine. I dont know whats the working with my application. there is no hidden javascript also :( – jestges Feb 24 '12 at 07:09
  • got it now. In page load there is another dynamic script which is accessing html control. Thank you for all your help. – jestges Feb 24 '12 at 07:17
  • 22
    Future visitors: Depending on how you trigger page reloading, Firefox 21.0 (maybe other browsers also) will honor the dropdown position you last selected via ui, even though the html has the selected="selected" attibute set on a different option tag. Ctrl-r, selected option tag is not shown as selected but your last gui selection is. Hit return on the page address field in the browser and the visible selection is reset to match the option with the selected attribute. Chrome 27.0.1453.116 did not show this same behavior. – Anatortoise House Jul 03 '13 at 22:17
  • For others with this problem, if you use CSS, check your padding property, I fixed it by removing/adjusting it. (Chrome 83.0.4103.119) – frogDoraTheRollerskatingFedora Jul 06 '20 at 18:03
  • Adding `autocomplete="off"` to the form tag fixes this for me. – user24601 Mar 15 '22 at 21:24

1 Answers1

76

You probably have something on your page selecting the value. You might be able to quickly verify that if the same value is still selected after changing the order of the options in the html...

You might want to refer to: Firefox ignores option selected="selected"

Community
  • 1
  • 1
David
  • 2,715
  • 2
  • 22
  • 31
  • 5
    Agreed. Browser autocomplete has tricked me a number of times before with HTML forms. +1 for disabling autocomplete as a solution. The attribute can be applied to
    or or
    – Charlie Dalsass Sep 11 '15 at 22:55
  • 2
    Ha! Of course! I have jquery setting the values when opening a dialogue and it cleared them when it was NULL. Sheesh... Thank you! – not_a_generic_user Nov 08 '17 at 21:59