How to add/remove class of child element on click of var ID?

Scott Blair

I have an accordion I'm using for a FAQ page. When a particular question is clicked, the section arrow needs to change from right to down. All others need to default back to right. What am I missing to get this to work?

            <dd id="1" class="accordion-navigation">
                <i class="fa fa-chevron-right"></i> <a class="faq-toggle" href="#faq1">How can I plan my Wedding at Mayan Resorts?</a>
                <div id="faq1" class="content">
                  Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
                </div>
            </dd>

            <dd id="2" class="accordion-navigation">
                <i class="fa fa-chevron-right"></i> <a class="faq-toggle" href="#faq2">Do you offer a service to plan the event start to finish?</a>
                <div id="faq2" class="content">
                  Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
                </div>
            </dd>



$('.accordion-navigation').click(function() {
        var id = $(this).attr('id');

                if ($(id).hasClass('active')) {
                        $(id).find('i').removeClass('fa-chevron-right');
                        $(id).find('i').addClass('fa-chevron-down');
                }
                else if (!($('.accordion-navigation').hasClass('active'))) {
                        $('.accordion-navigation').find('i').addClass('fa-chevron-right');
                        $('.accordion-navigation').find('i').removeClass('fa-chevron-down');
                }
});
Jaka Dirnbek

Do not know exactly why it doesn't work but I made up my own solution - CSS and JS combo

http://jsfiddle.net/95rzLepo/

$(document).on('click', '.faq-toggle' ,function (e) {
    if($(this).prev().hasClass('fa-chevron-down')) {
        $(this).prev().removeClass('fa-chevron-down');
        $(this).prev().addClass('fa-chevron-right');
    } else {
        $(this).parents('body').find('.fa.fa-chevron-down').each(function (e) {
            $(this).removeClass('fa-chevron-down');
            $(this).addClass('fa-chevron-right');
        });

        $(this).prev().removeClass('fa-chevron-right');
        $(this).prev().addClass('fa-chevron-down');
    }
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to mouse hover a parent element and subsequently click on child element using Selenium and Action class

From Dev

Jquery get id of an element on click of a common class

From Dev

How to click an element in xpath and selenium having option-id element but same div class?

From Dev

how to trigger click event on child element

From Dev

on click toggle for child element of the certain class (handlers) - jquery

From Dev

How to Click Parent Element Based on Child Image Element

From Dev

How to click a parent element using the child element for the following scenario

From Dev

Click not fired for child element

From Dev

How to remove element with id and class

From Dev

how to toggle the child element class using angularjs

From Dev

How to find a child html element by id with jsoup?

From Dev

How can I get the id of a child element/

From Dev

How to find parent id by click on child id and then child id by getting parent id via jquery

From Dev

How to add the `active` class on click of `li` element

From Dev

How to catch element click by ID in Android webview?

From Dev

How to catch element click by ID in Android webview?

From Dev

How to select first child of parent by using ID of a child element?

From Dev

How to Deativate parent element click event if child anchor tag is clicked?

From Dev

How to Deativate parent element click event if child anchor tag is clicked?

From Dev

How to set child of parent element click event target with meteor

From Dev

How to check if element with class is first-child of that class

From Dev

child with element of class

From Dev

Remove a class on a child element

From Dev

Getting child element of a child element of a parent with ID

From Dev

How can I exclude child class from click event?

From Dev

Javascript Click on Element by Class

From Dev

How to Get Element From Path Not Class or Id

From Dev

How to test an element with protractor that has no id or class?

From Dev

how to add class to element from its id?

Related Related

  1. 1

    How to mouse hover a parent element and subsequently click on child element using Selenium and Action class

  2. 2

    Jquery get id of an element on click of a common class

  3. 3

    How to click an element in xpath and selenium having option-id element but same div class?

  4. 4

    how to trigger click event on child element

  5. 5

    on click toggle for child element of the certain class (handlers) - jquery

  6. 6

    How to Click Parent Element Based on Child Image Element

  7. 7

    How to click a parent element using the child element for the following scenario

  8. 8

    Click not fired for child element

  9. 9

    How to remove element with id and class

  10. 10

    how to toggle the child element class using angularjs

  11. 11

    How to find a child html element by id with jsoup?

  12. 12

    How can I get the id of a child element/

  13. 13

    How to find parent id by click on child id and then child id by getting parent id via jquery

  14. 14

    How to add the `active` class on click of `li` element

  15. 15

    How to catch element click by ID in Android webview?

  16. 16

    How to catch element click by ID in Android webview?

  17. 17

    How to select first child of parent by using ID of a child element?

  18. 18

    How to Deativate parent element click event if child anchor tag is clicked?

  19. 19

    How to Deativate parent element click event if child anchor tag is clicked?

  20. 20

    How to set child of parent element click event target with meteor

  21. 21

    How to check if element with class is first-child of that class

  22. 22

    child with element of class

  23. 23

    Remove a class on a child element

  24. 24

    Getting child element of a child element of a parent with ID

  25. 25

    How can I exclude child class from click event?

  26. 26

    Javascript Click on Element by Class

  27. 27

    How to Get Element From Path Not Class or Id

  28. 28

    How to test an element with protractor that has no id or class?

  29. 29

    how to add class to element from its id?

HotTag

Archive