[Proto-Scripty] Re: Updating value of hidden form control
On Feb 24, 2:15 am, SWilk wrote: > Hector Virgen wrote: > > Hello, > > > I am writing a plugin that takes a text input () and > > hides it using $(input).hide(). The plugin then uses other controls to > > update the value of the text input. > > > I am using Element.writeAttribute() to update the value, but it does not > > seem to work when the input is hidden. However, using the old-fashioned > > value property to update it seems to work. Is there a reason why > > writeAttribute does not work with hidden elements, or should I be using > > a different method? > > I haven't checked any reference for this, but my from my experience, > the method Element#setAttribute('value','someValue'), used internally > by Element#writeAttribute method DOES NOT UPDATE the value at all. writeAttribute does use setAttribute which updates both the value attribute and value property in Firefox and IE. The issue is that in IE 6 (and perhaps later), it doesn't change the defaultValue property whereas it does in Firefox. > Instead it sets the ATTRIBUTE to the value provided. The attribute and the property are set to the same value. > When user clicks > "Reset" button, then the attribute value is re-read and put into > Element.value property. No, it isn't, the defaultValue property is used. When the input is first added to the DOM, it's value and defaultValue are set to the value of the HTML value attribute. When changing the value using setAttribute, Firefox changes both the value and defaultValue, however IE doesn't change the defaultValue. Consequently, in IE, when reset it used, the value is set to the original value (both the attribute and the property). > I use this to check if the form was modified since last save. Each > time I save the form with Ajax.Request, I also copy all > Element.value's to proper attributes. And before next save request, I > check if the values had changed, only then I submit the form. There is no need to do that, simply compare each control's value to its defaultValue. You'll save a bunch of CPU cycles too (not that your users will notice). > Anyway, what I want to tell, is that the element property, and element > attribute are not always the same. In the vast majority of cases they are identical, however there are some HTML attribute names that match reserved words in ECMAScript (class, for) and DOM properties that do not have an HTML attribute equivalent (such as defaultValue). My rule is to always use the DOM property when dealing with standard HTML attributes and only use get/setAttribute (or read/writeAttribute) for non-standard attributes (which I never use but sometimes I have to deal with such use by others). -- Rob --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: Updating value of hidden form control
On Feb 24, 3:50 am, Hector Virgen wrote: > Thanks for the clarification. I assumed that the attribute would be the same > as the element property, but I see now how they can be different. To change the default value, use the defaultValue property: http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-26091157 > -- Rob --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: Updating value of hidden form control
Thanks for the clarification. I assumed that the attribute would be the same as the element property, but I see now how they can be different. -Hector On Mon, Feb 23, 2009 at 8:15 AM, SWilk wrote: > > Hector Virgen wrote: > > Hello, > > > > I am writing a plugin that takes a text input () and > > hides it using $(input).hide(). The plugin then uses other controls to > > update the value of the text input. > > > > I am using Element.writeAttribute() to update the value, but it does not > > seem to work when the input is hidden. However, using the old-fashioned > > value property to update it seems to work. Is there a reason why > > writeAttribute does not work with hidden elements, or should I be using > > a different method? > > > > I haven't checked any reference for this, but my from my experience, > the method Element#setAttribute('value','someValue'), used internally > by Element#writeAttribute method DOES NOT UPDATE the value at all. > > Instead it sets the ATTRIBUTE to the value provided. When user clicks > "Reset" button, then the attribute value is re-read and put into > Element.value property. > > I use this to check if the form was modified since last save. Each > time I save the form with Ajax.Request, I also copy all > Element.value's to proper attributes. And before next save request, I > check if the values had changed, only then I submit the form. > > Anyway, what I want to tell, is that the element property, and element > attribute are not always the same. > > > Regards, > SWilk > > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: Updating value of hidden form control
Hector Virgen wrote: > Hello, > > I am writing a plugin that takes a text input () and > hides it using $(input).hide(). The plugin then uses other controls to > update the value of the text input. > > I am using Element.writeAttribute() to update the value, but it does not > seem to work when the input is hidden. However, using the old-fashioned > value property to update it seems to work. Is there a reason why > writeAttribute does not work with hidden elements, or should I be using > a different method? > I haven't checked any reference for this, but my from my experience, the method Element#setAttribute('value','someValue'), used internally by Element#writeAttribute method DOES NOT UPDATE the value at all. Instead it sets the ATTRIBUTE to the value provided. When user clicks "Reset" button, then the attribute value is re-read and put into Element.value property. I use this to check if the form was modified since last save. Each time I save the form with Ajax.Request, I also copy all Element.value's to proper attributes. And before next save request, I check if the values had changed, only then I submit the form. Anyway, what I want to tell, is that the element property, and element attribute are not always the same. Regards, SWilk --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: Updating value of hidden form control
On Feb 23, 1:00 pm, Hector Virgen wrote: > Hello, > > I am writing a plugin that takes a text input () and > hides it using $(input).hide(). The plugin then uses other controls to > update the value of the text input. > > I am using Element.writeAttribute() to update the value, but it does not > seem to work when the input is hidden. However, using the old-fashioned > value property to update it seems to work. Is there a reason why > writeAttribute does not work with hidden elements, or should I be using a > different method? If you know the "old fashioned" way works, and it is extremely likely that it always will as it is based on a fundamental javascript language feature, why would you use writeAttribute at all? -- Rob --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---