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

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!