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
unnamedunnamed2
Courses Plus Student 107 PointsHow to write a Javascript plugin
Hi, how to convert this piece of javascript functionality to a plugin, to reuse it without repeat code for each tab component
$(".tab-list").on("click", ".tab", function(e) {
e.preventDefault();
$(".tab").removeClass("active");
$(".tab-content").removeClass("show");
$(this).addClass("active");
$($(this).attr("href")).addClass("show");
});
4 Answers
rydavim
18,814 PointsWithout getting an idea of the rest of your code, I'm not sure I have a specific answer. But in terms of the process, there is a pretty good tutorial on the jQuery website.
Here is one possible solution to the problem of having clicks effect multiple elements. There are probably more elegant ways of writing this, but hopefully this'll give you an idea of where to start.
$(".tab-list").on("click", ".tab", function(e) {
e.preventDefault();
// I setup variables for readability.
const index = $(this).index();
const tabs = $(this).closest(".tabs");
const children = tabs.children(".tab-content");
$(this).siblings().removeClass("active"); // Remove the active class from sibling a elements.
children.removeClass("show"); // Remove the show class from associated tab content elements.
$(this).addClass("active"); // Make the current tab active.
$(children[index]).addClass("show"); // Show the corresponding tab content.
});
If you have any follow up questions, please let me know. Happy coding!
unnamedunnamed2
Courses Plus Student 107 Pointsrydavim, this is the full code https://codepen.io/taniarascia/pen/EZwdNg
rydavim
18,814 PointsSorry, I guess I'm not sure what code you're worried about repeating? While your code could probably be made more succinct if you wanted, I'm not sure that a plugin implementation would really help here. I don't have much experience with jQuery plugins specifically, but for what you're doing it seems like you'd still need to bind the event handler?
unnamedunnamed2
Courses Plus Student 107 Pointsrydavim , when I have two or more components the code breaks https://codepen.io/anon/pen/Xvpbgv
rydavim
18,814 PointsOkay, got it. So in this case, it has to do with the fact that you're reusing html ids. HTML id attributes are meant to be unique, and should not be repeated. I'm not particularly familiar with codepen, but I'm a little bit surprised it didn't warn you about the conflict.
I have update my original answer with an example of one possible solution.
unnamedunnamed2
Courses Plus Student 107 PointsThank you very much!