On Jan 26, 4:52 pm, kangax <[EMAIL PROTECTED]> wrote: > I have added a tickethttp://dev.rubyonrails.org/ticket/10930 > > So, writeAttribute('selected') should set selected to true. Do we > still need to assign an attribute via setAttribute('selected', > 'selected')?
No, writeAttribute is a wrapper for the built-in setAttribute method of the DOM 2 Core Element interface. <URL: http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-F68F082 > If the intention is to make a particular option the currently selected option, setAttribute is bad way to go about it. setAttribute (and hence writeAttribute) sets the value of the element's HTML selected property, which does not set an option element as the currently selected option in some browsers (Safari 3, Firefox 2, Opera 9) but does in others (IE 6). The HTML selected attribute is typically only used to select an option when the HTML is first parsed and when a form is reset if the select is in a form. Hence using setAttribute may well caues multiple option elements to have their selected attribute set to true in a single- select element - the result of that is browser dependent and unreliable as indicated above. You can only reliably set an option to be the currently selected option by either setting the selectedIndex property of the select element OR setting the selected property of the option element directly, i.e.: selectElement.selectedIndex = <some option.index value>; OR optionElement.selected = true; Note that you can also the selectedIndex property it to -1 to have no option selected. Use either of the above methods and hasAttribute(selected) will return false, unless the option element's selected attribute has been set previously (see below). > I understand that hasAttribute('selected') should return > true on such element, but would it actually have any meaning? hasAttribute uses the DOM 2 Core getAttributeNode method, which returns true or false depending on whether a value has been specified for the attribute. In this case, using setAttribute to set the value of the attribute to either 'true' or 'false' will cause hasAttribute to return 'true', since the attribute has been assigned a value, even though the value might be false. So yes, it's useless for determining whether a particular option is the currently selected option. To do that reliably, you need to either check whether the option's index property has the same value as the select's selectedIndex property, or check the selected property of the option directly, e.g. if (optionElement.index == optionElement.parentNode.selectedIndex) or if (optionElement.selected) The second method is reliable if the selected attribute has been set in the source HTML, but unreliable in some browsers if setAttribute has been used to mess with the HTML selected property. The bottom line in all that is: do not use writeAttribute (or setAttribute) to set an option as the currently selected option. Set either the selectedIndex property or the option's selected property directly. Following is some code, play with it in different browsers - it's a reall spinout. <select id="xx"> <option id="opt0">zero <option id="opt1" selected>one <option id="opt2">two </select><br> <button onclick=" $('xx').selectedIndex = -1; ">('xx').selectedIndex = -1</button><br> <button onclick=" $('xx').selectedIndex = 2; ">('xx').selectedIndex = 2</button><br> <button onclick=" $('opt2').writeAttribute('selected', 'selected'); ">('opt2').writeAttribute('selected', 'selected')</button><br> <button onclick=" $('opt2').writeAttribute('selected', true); ">('opt2').writeAttribute('selected', true)</button><br> <button onclick=" $('opt2').setAttribute('selected',false); ">$('opt2').setAttribute('selected',false)</button><br> <button onclick=" $('opt2').selected = true; ">('opt2').selected = true;</button><br> <button onclick=" alert($('opt2').hasAttribute('selected')); ">alert($('opt2').hasAttribute('selected'))</button><br> <button onclick=" alert($('opt1').selected); ">alert(opt1.selected)</button><br> <button onclick=" alert($('opt2').selected); ">alert(opt2.selected)</button><br> -- Rob --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
