1

I have a link function in an directive where i have a toggle functionality to edit the currently active DOM element's css content . How do i access the parent or child class of that DOM element .

A part of the directive :

   link: function(scope, element, attrs) {

        var title = angular.element(element.children()[0]),
            opened = true;

        title.bind('click', toggle);

        function toggle() {
            opened = !opened;
                element.removeClass(opened ? 'closed' : 'opened');                  
                element.addClass(opened ? 'opened' : 'closed');
            element.siblings().removeclass('opened').addclass('closed');  // What to do here  

        }
4
  • Better would be to post your relevant code Commented Jan 3, 2013 at 10:23
  • meaning Currentlty active DOM elements siblings ... Commented Jan 3, 2013 at 10:24
  • You can edit your question, you know... Commented Jan 3, 2013 at 10:29
  • @asgoth : scope.$apply didnt work ...it took more time to load the file in the browser after i changed and angular binding to the front end failed Commented Jan 3, 2013 at 10:56

1 Answer 1

1

I would change your link function to this:

link: function(scope, element, attrs) {
    var title = angular.element(element.children()[0]),
        opened = true;

    title.bind('click', toggle);

    function toggle() {
        opened = !opened;
        element.parent().children().addClass("closed");
        element.parent().children().removeClass("opened");
        element.toggleClass("opened", opened);
        element.toggleClass("closed", !opened); //might be redundantdepending on the rest of your code

    }
}

I think you're doing it in the wrong order, if you first remove/add all the classes you need and then modify the element you'll get the result you're after. Simply select the elements parent, and then get all it's parent children. .children() selects only the direct descendents of the parent, while the .find() method which is similar would traverse down the hierarchy. The redundant code might not be necessary depending on if there are other ways to close the element.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the suggestion @MatthewBerg ...I ll look through that :)
What do i do if i already have a "closed" class for one of the children elements ... ie if the function toggle already took place for that element , then a closed class would already exist ... > element.parent().children().addClass("closed"); then wouldnt i be adding the "closed" class again
No, it will just leave 1 closed class. If an element already has a class and you try to add it again it won't duplicate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.