views:

487

answers:

2

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

+1  A: 

$(document.body).select('li') returns a collection, not an element, right? I would assume you want:

    if (element.hasClassName('yes')) {
        element.removeClassName('yes');
    }

However, it seems that your logic is flawed -- you are first adding the class if the input is checked, then you are immediately removing it. Are you missing an else? Maybe something more like:

function init() {
    $(document.body).select('input').each(function(element) {
        if (element.checked) {
            element.up('li').addClassName('yes');
        }
        else {
            element.up('li').removeClassName('yes');
        }
    })
}
bmoeskau
Yes but it should, act on load, check on page load if there is a checked element, and assign the 'yes' class to it (or remove if there is no checked elements but they have class 'yes'). But on the same time, in the next code, if there is a click event, if an input is checked, it should add the 'yes' class to the parent li, and if you uncheck an input (checkbox), it should remove the class 'yes'This is the code that I have used for events observers
BoDiE2003
document.observe("dom:loaded", function() { $(document.body).select('input').each(function(element) { element.observe('click', function() { init(); }); init(); });});
BoDiE2003
This one is working, but I had to use element.up().removeClassName('yes') without the element.up('li') - without the li I mean.
BoDiE2003
Glad you got it figured out.
bmoeskau
A: 

Wait a sec - that "select" is going to return an array of elements. The "hasClassName" function is a function on Element directly, not on Array or Enumerable. You're missing an "each()" layer.

$$('li').each(function(li) {
  if (li.hasClassName('yes')) li.removeClassName('yes');
});
Pointy
Check the previous answer:Yes but it should, act on load, check on page load if there is a checked element, and assign the 'yes' class to it (or remove if there is no checked elements but they have class 'yes'). But on the same time, in the next code, if there is a click event, if an input is checked, it should add the 'yes' class to the parent li, and if you uncheck an input (checkbox), it should remove the class 'yes' This is the code that I have used for events observers –
BoDiE2003