Javascript: addClassName if input is checked and removeClassName if it was un-checked

Posted by BoDiE2003 on Stack Overflow See other posts from Stack Overflow or by BoDiE2003
Published on 2010-05-14T15:26:42Z Indexed on 2010/05/14 15:44 UTC
Read the original article Hit count: 340

Filed under:
|
|
|
|

Im trying to select all the li tags of the document and check if it hasClassName('yes') so if it has, it will remove it. But im having a TypeError: Object [object HTMLLIElement], has no method 'hasClassName' error.

This is the DOM method:

document.observe("dom:loaded", function() {
    $(document.body).select('input').each(function(element) {
        element.observe('click', function() {
            init();
        });
        init();
    });

});

The previous code will take the init fuction and check the if there are checked inputs and add them the 'yes' class name, but if I un-check those inputs, the class remains. This is the funcion that Im trying to do dynamic (add and remove class 'yes');

function init() {
    $(document.body).select('input').each(function(element) {
        if (element.checked) {
            element.up('li').addClassName('yes');
        } 

        if ($(document.body).select('li').hasClassName('yes')) {
            element.removeClassName('yes');
        }
    })
}

Can you help me solving the last part of this function, so the removeclassname method will work? Thank you

© Stack Overflow or respective owner

Related posts about prototype

Related posts about JavaScript