Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Dave Six
8,366 PointsSelecting single selector generated in loops
Hello everyone,
I have to write a code which selects single accordion-elements and text elements on a website which open a content box.
The accordion-elements can also be triggered by external text elements - unfortunately if I am performing a console.log() with the variables for the click event, it only shows the last selector which got generated in the loops.
It shows up on every clickable text element I click on.
Here's my code so far
for (var panel = 0; panel <= 10; panel++) {
for (var level = 1, acc = 1; level, acc <= 5; level++, acc++) {
var link = ".acc_link_" + level + "_" + panel;
var selector = "#ui-accordion-" + acc + "-panel-" + panel;
jQuery(link).click(function () {
jQuery(selector).toggle(400);
console.log(selector);
console.log(link);
});
}
}
I Also have a working code but it is horribly written and not dynamic at all since I am pretty new to JavaScript and jQuery
if (jQuery("#ui-accordion-1-panel-1")) {
jQuery(".acc_link_1_1").click(function () {
jQuery("#ui-accordion-1-panel-1").toggle(400);
});
}
8 Answers
Dave McFarland
Treehouse TeacherThis is a common problem with functions -- what's happening is that the click event is given an anonymous function that only runs AFTER the loop has run. In other words, the variables link and selector within the click function for all the links will always be the same, and will always be set to .acc_link_5_10 and #ui-accordion-5-panel-10. There are a few ways around this. I think the simplest is to use jQuery's .data method to add data to each link within the loop. Then extract the data in each click using $(this). Here's a stab -- I don't have your HTML markup so I can't test this, but let us know if it works:
for (var panel = 0; panel <= 10; panel++) {
for (var level = 1, acc = 1; level, acc <= 5; level++, acc++) {
var $link = $(".acc_link_" + level + "_" + panel);
$link.data('selector', "#ui-accordion-" + acc + "-panel-" + panel);
$link.click(function () {
var $this = $(this)
$this.data('selector').toggle(400);
console.log($this.data('selector'));
console.log($this.attr('class'));
});
}
}
This works because the click function is bound to each of the accordion links and $(this) refers to the specific accordion link clicked on. Because the data is stored in the link within the loop, you can extract it with $(this).data().
bobkingstone
27,869 PointsHi
Is there any chance of uploading the working example to codepen so we can see what you are trying to achieve? its a little hard to visualise at the moment.
Regards Bob
Dave Six
8,366 PointsSince this is not just an HTML file but Wordpress which is working behind the scene I'm not sure if I'm able to upload that (I'm not familiar with codepen as well so if I am wrong please correct me). The only option I could offer you is showing you a screenshot how it's supposed to look like.
bobkingstone
27,869 PointsUnfortunately your images come up 403.
Bob
Dave Six
8,366 PointsSry - this should work https://www.dropbox.com/sh/cmkys4qnjgykc1i/AACmk5jKvJYPe5aXGbG8vs8fa
bobkingstone
27,869 PointsSorry but without seeing the code I can't really help you on this.
One suggestion i would have is you could create a named function that clicking the accordion value calls, passing it the section to open. That way you could target individual sections.
Bob
Dave Six
8,366 PointsWell sorry that I couldn't upload the code. Still thanks a lot for your support Bob!
Dave Six
8,366 PointsHello Dave
Thanks a lot for post! I had a hard time figuring out why my variables were always set to the last value they got. The code does not work yet but with your advice I should get things fixed.