Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

Checkbox and the #set method

#162

seems like it doesn't work with checkboxes

$('that-box').set('checked', true);

ain't producing anything in webkit. needs some investiation

Reported by MadRabbit · January 6th, 2010 @ 10:31 PM

State: resolved
Milestone: 1.5.3 Release
Assigned to: nobody

Activity

  1. Douwe M.
    Douwe M.

    Apparently WebKit doesn't like

    element.checked = true;
    
    But does like:
    element.setAttribute('checked', 'checked');
    

    January 7th, 2010 @ 02:40 PM

  2. MadRabbit
    MadRabbit

    I didn't investigated the problem yet, but I think element.checked = true; is working, it have to. the problem is probably in the way how the #set method works.

    January 7th, 2010 @ 03:23 PM

  3. Douwe M.
    Douwe M.

    Well,

    a = document.createElement('input'); 
    a.setAttribute('type', 'checkbox');
    a.checked = true;
    
    results in <input type=​"checkbox">​, where
    a = document.createElement('input'); 
    a.setAttribute('type', 'checkbox');
    a.setAttribute('checked', 'checked');
    
    results in <input type=​"checkbox" checked="checked">​, as expected.

    January 7th, 2010 @ 05:42 PM

  4. MadRabbit
    MadRabbit

    There is a difference for input elements between a value/state assignment and attribute settings. You can change the element value, but the attribute will be the same, which is basically used for form resets. but if you don't change the attribute, it doesn't mean that you won't change the actual value.

    so in that case

    a = document.createElement('input');
    a.type = 'checkbox';
    a.checked = true;

    although you won't see the attributes, it still will be a checkbox and it will be checked. Same if you would do it with a normal input element

    a = document.createElement('input');
    a.type = 'text';
    a.name = 'name';
    a.value = 'bla bla bla';

    when you insert this on the page you'll see "" in the content, but in reality, your input element will have all the correct name, value and type, which will be used if you submit a form.


    Don't worry about that, this ticket is a simple fix, just the #get method is a performance sensitive part, and I'll need to do some regressive testing first.

    January 7th, 2010 @ 06:07 PM

  5. Douwe M.
    Douwe M.

    Hmm, I feel really stupid now... :-( I know all of this, but for some reason I didn't think of it :|

    But have you found the cause for this problem?

    January 7th, 2010 @ 06:17 PM

  6. MadRabbit
    MadRabbit

    I think it's because my #set method don't convert data in strings, so when you say #set('checked', true) it will call
    element.setAttribute('checked', true); and that probably causes the problem.

    I could make it simply element.setAttribute('checked', ''+value); but not sure if it's the most efficient way of doing that

    January 7th, 2010 @ 06:24 PM

  7. Douwe M.
    Douwe M.

    This is definition for the Element#set method:

      set: function(hash, value) {

    if (value) { var val = {}; val[hash] = value; hash = val; }
    
    for (var key in hash) {
      // some attributes are not available as properties
      if (this[key] === undefined) {
        this.setAttribute(key, ''+hash[key]);
      }
      this[key] = hash[key];
    }
    
    return this;
    
    
    
    
    }
    So what happens is it checks if this['checked'] isn't undefined, i.e. is an available property, which it is, and thus doesn't even get to the this.setAttribute part, it only sets this['checked'] to true.

    This means you're guess is incorrect :P Unless my brain is screwing up again...

    January 7th, 2010 @ 06:30 PM

  8. MadRabbit
    MadRabbit

    Well, that's why it's called a "guess", if I knew for sure I would probably just fixed it

    I'm not sure what's the problem and if there is a problem at all. I'm busy with the documentation now and don't want to deal with it now, I had run in an issue in some script and simply created the ticket, so that I didn't forget about it later.

    January 7th, 2010 @ 07:01 PM

  9. Douwe M.
    Douwe M.

    Yeah, that's true. But guesses are most of the time, well, guesses to what the problem could be. I'm just trying to help you by pointing out one isn't very probable :-)

    January 7th, 2010 @ 07:04 PM

  10. MadRabbit
    MadRabbit

    I know, you're doing fine. Just I cannot do all the things at once 8)

    If you're interested in the bug and have time, you could investigate it, maybe there is no bug at all and it was just me getting screwed with that my script.

    write couple of scripts setting the property to true and false, with the constructor and with the method, and run them in different browsers, see what will happen.

    January 7th, 2010 @ 07:13 PM

  11. Douwe M.
    Douwe M.

    I will when I have the time :-)

    January 7th, 2010 @ 07:20 PM

  12. MadRabbit
    MadRabbit
    • State changed from new to resolved

    okay, the problem was that I was trying to set the 'checked' value to 'false' like that

    $('checkbox').set('checked', false);
    

    and then the front converter

    if (value) { var val = {}; val[hash] = value; hash = val; }
    

    didn't convert the two variable arguments into a hash, because had a boolean check. i've changed it for

    if (typeof(hash) === 'string') ....
    

    and now it works fine

    January 8th, 2010 @ 01:07 PM

  13. MadRabbit
    MadRabbit
    • Milestone set to 1.5.3 Release

    January 10th, 2010 @ 02:07 PM

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.