Closed Bug 246647 Opened 22 years ago Closed 22 years ago

After page reload my javascript doesn't move selected option from srs to destination select.

Categories

(Core :: DOM: Core & HTML, defect)

x86
Linux
defect
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: alonzi, Unassigned)

Details

User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 Build Identifier: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 My page has 2 select multiple (src,dst) src has 2 option, xxx and yyy. yyy is selected. I wrote a js function that moves selected options from srt to dst. When I load the page, yyy is moved to dst select. Instead when I reload the page dst select is empty. Reproducible: Always Steps to Reproduce: 1.load the page. 2.Press reload button. Actual Results: After reload dst select is empty. Expected Results: src contains xxx and dst contains yyy. html code: <html> <head> <script type="text/javascript"> function moveSelectedOptions(from,to) { // Move them over for (var i=0; i<from.options.length; i++) { var o = from.options[i]; if (o.selected) { to.options[to.options.length] = new Option( o.text, o.value, false, false); } } // Delete them from original for (var i=(from.options.length-1); i>=0; i--) { var o = from.options[i]; if (o.selected) { from.options[i] = null; } } } </script> </head> <body> <form action="xxx.php" method="post"> <select name="list11" multiple="multiple" size="10"> <option>xxx</option> <option selected>yyy</option> </select> <select name="participants[]" multiple="multiple" size="10"> </select> <script type="text/javascript"> moveSelectedOptions(document.forms[0]['list11'], document.forms[0]['participants[]']); </script> </form> </body> </html>
Reporter: please file DOM bugs appropriately, not against the JS engine. Are [ and ] legal in name attribute values? /be
Assignee: general → general
Component: JavaScript Engine → DOM: Level 0
Summary: After page reload my javascript doesn't move selected option from srs to destination select. → After page reload my javascript doesn't move selected option from srs to destination select.
Reporter, there are a lot of problems with the code you provided. 1- A new Option() call will only work in MSIE. DOM 2 HTML recommends createElement("option") method and ObjRefSelect.add() method. See these links: http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-2141741547 http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-14493106 http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-33404570 http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-70901257 You can use createElement("option"), createTextNode() and appendChild() and you'll be able to populate select accordingly in MSIE 6, Mozilla-based browsers, Opera 7.x and countless other W3C compliant browsers, and this, without writing any cross-browser code. 2- Your code has other problems too. <select name="participants[]" multiple="multiple" size="10"> </select> a) The name "participants[]" uses [] which may possibly create problems; best is to avoid special characters like these b) the select requires at least 1 option: the validator will report an error on this. "A SELECT element must contain at least one OPTION element." http://www.w3.org/TR/html401/interact/forms.html#edef-SELECT 3- You're using an year old Mozilla-based browser to report a bug. "(...) be sure that you've reproduced your bug using a build released within the past three days. Our development process moves at lightning speed, and the bug you've found may already have been fixed." http://www.mozilla.org/quality/bug-writing-guidelines.html I'm resolving this as INVALID.
Status: UNCONFIRMED → RESOLVED
Closed: 22 years ago
Resolution: --- → INVALID
drunclear: INVALID may be the right resolution, but new Option should work -- it's part of DOM level 0, has been around since Netscape 3, and although not ever spec'd by the w3c, should continue to be supported. jst, what do you say? /be
(In reply to comment #2) > Reporter, there are a lot of problems with the code you provided. > > 1- A new Option() call will only work in MSIE. DOM 2 HTML recommends > createElement("option") method and ObjRefSelect.add() method. See these links: Your approach can be better. I'll try it but new Option is a valid method. when I load the page the first time, why does the scripts works ? > 2- Your code has other problems too. > <select name="participants[]" multiple="multiple" size="10"> > </select> > a) The name "participants[]" uses [] which may possibly create problems; best is > to avoid special characters like these It can't be a problem. If I want to send more option to a php script I must use this notation. > b) the select requires at least 1 option: the validator will report an error on > this. > "A SELECT element must contain at least one OPTION element." > http://www.w3.org/TR/html401/interact/forms.html#edef-SELECT Ok! > 3- You're using an year old Mozilla-based browser to report a bug. > "(...) be sure that you've reproduced your bug using a build released within the > past three days. Our development process moves at lightning speed, and the bug > you've found may already have been fixed." > http://www.mozilla.org/quality/bug-writing-guidelines.html > > I'm resolving this as INVALID. I test the problem with firefox 0.8 Bye
(In reply to comment #3) > drunclear: INVALID may be the right resolution, but new Option should work -- > it's part of DOM level 0, has been around since Netscape 3, and although not > ever spec'd by the w3c, should continue to be supported. We do support both |new Option()| and |new Image()|. If they don't work there's a bug that should be filed separately, but my limited testing shows they work just fine.
Regarding new Option(), I verified with this file http://devedge.netscape.com/library/xref/2002/client-data/property-data-window.html before resolving the bugfile and I was mislead. <shrug> Nothing about new Option() at Gecko DOM reference either. I certainly never used new Option() before. Please accept my apologies about that, Alonzi. I still think that when one can use exclusively DOM 2 methods, one should do it. You can use special characters for name attribute values but I recommend against that. Finally, I think I've found the problem with your code. Replace this statement (twice in your code) if (o.selected) with if (o.defaultSelected) and then your code will work in Mozilla 1.7 and in MSIE 6 (which it was not before). I can upload a verified and working testcase about this if needed. "defaultSelected: Specifies the *initial* selection state of the option" http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/option.html#1193136 "selected: Specifies the *current* selection state of the option"
You need to log in before you can comment on or make changes to this bug.