From: Garrett Smith on 20 Dec 2009 13:36 Matt Kruse wrote: > On Dec 19, 6:40 pm, Garrett Smith <dhtmlkitc...(a)gmail.com> wrote: >> You probably tried reading the selectedIndex, and then found that >> worked, then tried to read the option.selected, and then found that >> worked, and then figured that by simply accessing selectedIndex, the >> selected property was achieved. Did I get that right? > > Me personally? No. I didn't come up with this. I asked about the > rationale for that segment of code because it never made sense to me. > >> Then you went to propose the workaround as a solution. That's a hack. > > That hack was there, I just suggested a way to improve the comment and > functionality of the hack. > I see. >> The problem was identified as: The default OPTION in a non-rendered >> SELECT is false, when the option will be selected by default. >> That is not a bug. > > I agree, but it is behavior that is different than other tested > browsers. In the case of jQuery, it is attempting to "normalize" > browsers, and since they found an exception probably in some rare case > that one user had, they tried to fix it. Personally, I don't feel this > section of code deserves to be in there at all. > Me neither. Adding the selected attribute fixes the problem and is a recommended practice in HTML 4.01. >> function updateOptionSelected(elem) { >> // If we're getting a false value, it may be that the option >> // will be selected when fully rendered, but has not happened yet. >> if(elem.selected === false) { >> var selectedOption = elem.parentNode.options[s.selectedIndex]; >> selectedOption.selected = true; >> } >> } >> Doesn't that make teh loop a lot clearer? > > Certainly, it looks like a better approach. But it would need to be > cleaned up to consider the possibility that an <option> might in an > <optgroup>. > Yeah, it should avoid error where `selectedOption` was undefined, such as where s.selectedIndex is -1. > Perhaps something like this: > > if (prop=="selected") { > return getOptionSelected(elem); > } Sure. > function getOptionSelected(el) { > var s = document.createElement("select"); > s.add(new Option("a")); > if (s.options[0].selected) { > return (getOptionSelected = function(opt) { return opt.selected; }) > (el); > } s = null; > // If we're getting a false value, it may be that the option > // will be selected when fully rendered, but has not happened yet. > return (getOptionSelected=function(opt) { > if(opt.selected === false) { > var s = (opt.parentNode.tagName=="SELECT")? > opt.parentNode:opt.parentNode.parentNode; var s = findAncestorWithTag(opt, "SELECT"); > if (s.type=="select-one" && s.selectedIndex>=0) { Why check s.type? > s.options[s.selectedIndex].selected=true; > } > } > return opt.selected; > })(el); > } > >> Your proposed workaround relies on an even bigger non-standard quirk. It >> is possible that bigger quirk (your workaround) will not be present in >> Safari 5 and the smaller quirk will remain. If and when that happens, >> you're back to trying to figure out the problem. > > Possible. Either way, the above solution seems more robust if one were > really interested in dealing with this quirk. > Yes. > And just to be clear - this is not "my proposed workaround". I was > exploring the problem to understand why those lines have existed in > jQuery for so long. Out of curiosity. > Got it. -- Garrett comp.lang.javascript FAQ: http://jibbering.com/faq/
From: Hans-Georg Michna on 21 Dec 2009 04:59 On Sat, 19 Dec 2009 21:44:12 -0800, Garrett Smith wrote: >What would a developer who understands the technology want jQuery for? Shorter, nicer-looking, easier-to-understand code perhaps? I must admit that I like jQuery's functional notation very much. I wish I had a version of jQuery that also worked perfectly under the hood. Haven't had any problems with jQuery yet, but that may be because I didn't do very deep things with it and didn't test with many older browsers. Hans-Georg
From: rf on 21 Dec 2009 05:07 "Hans-Georg Michna" <hans-georgNoEmailPlease(a)michna.com> wrote in message news:v7hui5la535o3haa1uo53odlpch1jj27mj(a)4ax.com... > On Sat, 19 Dec 2009 21:44:12 -0800, Garrett Smith wrote: > >>What would a developer who understands the technology want jQuery for? > > Shorter, nicer-looking, easier-to-understand code perhaps? > > I must admit that I like jQuery's functional notation very much. > I wish I had a version of jQuery that also worked perfectly > under the hood. So why don't you write your own scripting language? > Haven't had any problems with jQuery yet, but that may be > because I didn't do very deep things with it and didn't test > with many older browsers. Or many newer ones.
From: Matt Kruse on 21 Dec 2009 09:17 On Dec 20, 12:36 pm, Garrett Smith <dhtmlkitc...(a)gmail.com> wrote: > > function getOptionSelected(el) { > > var s = document.createElement("select"); > > s.add(new Option("a")); > > if (s.options[0].selected) { > > return (getOptionSelected = function(opt) { return opt.selected; }) > > (el); > > } > s = null; Good eye. > > if (s.type=="select-one" && s.selectedIndex>=0) { > Why check s.type? I was initially being explicit about only doing this for single- selects, but with the selectedIndex check, it is unnecessary since selectedIndex==-1 for multiple selects. I'm going to summarize the points discussed here and the code above to the jquery-dev list, although I doubt they will be receptive to such a change when the two-line code they have now "works good enough". Matt Kruse
From: Hans-Georg Michna on 21 Dec 2009 09:47
On Mon, 21 Dec 2009 10:07:21 GMT, rf wrote: >"Hans-Georg Michna" <hans-georgNoEmailPlease(a)michna.com> wrote in message >news:v7hui5la535o3haa1uo53odlpch1jj27mj(a)4ax.com... >> On Sat, 19 Dec 2009 21:44:12 -0800, Garrett Smith wrote: >>>What would a developer who understands the technology want jQuery for? >> Shorter, nicer-looking, easier-to-understand code perhaps? >> >> I must admit that I like jQuery's functional notation very much. >> I wish I had a version of jQuery that also worked perfectly >> under the hood. >So why don't you write your own scripting language? Have to make a living. (:-) >> Haven't had any problems with jQuery yet, but that may be >> because I didn't do very deep things with it and didn't test >> with many older browsers. >Or many newer ones. OK, I haven't done any very thorough tests. The critical point here is that many people feel a need for something like jQuery, and not all of them understand all the implications. Hans-Georg |