0

Been scratching my head on this one for a while. Basically I have an external div container and when the click is fired on that div I am trying to change the following under it: div class="selected" (which works) the ul.more i am trying to slide toggle and the icon i am trying to switch the class so .toggleClass

The ul and icon are not firing. Not sure what i am doing wrong here. And weirdly enough when i switch the trigger from the div to the icon I can actually toggle the class on the icon and the hidden ul as well. but for some reason when i make my trigger the div both proceeding it fail.

Jfiddle:http://jsfiddle.net/LDad5/1/

code below:

css:

ul.more {
display: none;
}

HTML:

<div class="wtf"> <div> <i class="icon-ygg-chevron-right"><span class="docu-title">Lorem ipsum dolor sit</span></i> <ul class="more actu-landing-list"> <li>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras quis mauris id dolor tincidunt cursus. Aliquam elementum malesuada purus, at porttitor risus viverra quis. In sit amet sagittis nibh. Suspendisse hendrerit mi risus, a rhoncus nunc ultricies quis. Nullam a posuere felis. Nunc aliquam odio mauris, ut porta diam cursus a. In elementum molestie sem, id aliquet nunc dignissim nec. Sed in elit a elit faucibus accumsan a vel dui. Sed vel ante et mauris tempor accumsan. Cras velit nunc, tempor nec venenatis non, ullamcorper at diam. Donec non odio aliquet, fermentum erat eget, rhoncus nisi. Pellentesque urna neque, mattis a consectetur volutpat, tincidunt non nisl. Curabitur pulvinar viverra tellus, faucibus hendrerit arcu elementum nec.</li> </ul> </div> </div>

JS:

$(document).ready(function() {
    $('.wtf div').on('click', changeCarrotClass3);
});

function changeCarrotClass3() {


    $(this).toggleClass("selected");

    $(this).next('ul').slideToggle(400);

    $(this).next('i').toggleClass("icon-ygg-chevron-down");

}
4
  • and also here is the other version i was originally trying to get to work. I was using the icon as the trigger and wanted to change the class of the prev div. i get the icon and the ul to fire but not the prev div Commented Jul 24, 2014 at 20:25
  • forgot jfiddle link: jsfiddle.net/L38XG/1 Commented Jul 24, 2014 at 20:26
  • There is no <tr> in the example fiddle in your previous comment. If it's an ancestor of your div, use .closest() instead, as .prev() only looks at the one, previous element; it doesn't go up through the DOM. Commented Jul 24, 2014 at 20:28
  • I see. typo: jsfiddle.net/L38XG/2 still the same result tho. does not fire. anywho let me look into closest. and retry. Commented Jul 24, 2014 at 20:34

1 Answer 1

1

.next() literally only looks at the next element. You want to use .find() which looks at descendants, since <i> and <ul> are descendants of the div being clicked on:

function changeCarrotClass3() {
    $(this).toggleClass("selected");
    $(this).find('ul').slideToggle(400);
    $(this).find('i').toggleClass("icon-ygg-chevron-down");
}

jsFiddle example

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

3 Comments

is there a way to find parents? so if i did use the icon as the trigger i could find the parent div in this case?
See my comment to your question above. You should read the docs to see the differences between .next/.find and .prev/.closest.
so i ended up using the icon as the trigger. The ul had to use the .next and the parent div i was able to use the .closest appreciate the help my man. Thanks

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.