Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

How Can I Concatenate this jQuery Code?

Hey guys,

So I'm super new to the JavaScript and jQuery world and I'm trying to concatenate the below code so I don't do the DRY principal in my code.

$('ul.nav-tabs a').click(function(e) {
    e.preventDefault();
    $(this).tab('show');
});
$('ul.nav-rounds a').click(function(e) {
    e.preventDefault();
    $(this).tab('show');
});

I have tried the following but I obviously get an error message .

$('ul.nav-tabs a').click(function(e),
$('ul.nav-rounds a').click(function(e) {
    e.preventDefault();
    $(this).tab('show');
});

Any advice on how I can correctly concatenate my code will be greatly appreciated!

Thanks in advance

Stu : )

2 Answers

Comma separated click functions won't work which is likely why it's throwing an error. Since both of your anchors have the same functionality, you should be able to just combine them to one click function, comma separated. Have you tried:

$('ul.nav-tabs a, ul.nav-rounds a').click(function(e) {
    e.preventDefault();
    $(this).tab('show');
});

Hey mikes02,

Thanks for that now it makes total sense!

Stu : )

You're welcome, glad it worked out!