$('mycheckbox').set('checked', true);
is the 'correct' way, or maybe the mootools way.
Fábio Miranda Costa
Engenheiro de Computação
http://meiocodigo.com
On Tue, Mar 17, 2009 at 2:54 PM, Vittorio <[email protected]>wrote:
>
> I already post a teaser to this question, and now I'm going to fully
> explain.
>
> I'm creating controls in a form dinamically, and I want to set their
> initial state.
> In particular checkboxes:
>
> $('mycheckbox').checked=true; // I don't like this, breaks simmetry
> with the rest of the code
>
> $('mycheckbox').set('checked','checked');
>
> $('mycheckbox').setProperty('checked','checked');
>
> $('mycheckbox').setAttribute('checked','checked'); //works the best,
> but why?
>
> Didn't try the first, but have no reason not to think it works. So
> let's say we have FOUR ways of setting a checkbox state (unfortunate
> html creators: value should be a boolean, but is an optional string)
>
> BUT only the fourth - setAttribute - works fine with RESET buttons.
> All other sets "visually" the state of the checkbox, but don't change
> the DOM (inspecting it with fb) and when RESET is pushed the checkbox
> revert to unchecked.
>
> Has anyone the insight of this mechanism? Can he/she explain? How
> shall we code an plan about this issue?
>
> Thank you
>
>
> PS I know it's not the place but I want to tell you a trick I
> "invented" with checkboxes (receving data in php - where "0" string is
> falsy).
>
> To always have a value wether true or false I used to:
> <input type="hidden" name="thefieldname" value="0">
> <label>A checkbox
> <input type="checkbox" name="thefieldname" value="1">
> </label>
> eventually putting checked="checked" where it belonged.
>
> It happens somehow that the second checkbox, if chosen, overwrites the
> first.
>
> Maybe this could be implemented in a plugin for mootools, to
> "standardize" the way checkboxes are used.
> Thank you