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

María Pérez de Arrilucea
María Pérez de Arrilucea
6,490 Points

Method 'isValidEmail', why $(this) is not valid?

Hi, I know what is the correct answer, but I would like to know why the following code is not correct:

function isValidEmail(email) {
    return $(this).indexOf("@") != -1;
};

I thought $(this) would be related to the email.

Thanks a lot!

2 Answers

Matt Campbell
Matt Campbell
9,767 Points

Hi, no.

$(this) relates to the element that the function is relating to. So if you had a click function that was something like

$('#activator').click(function(){
    $(this).animate({...});
});

When you click on #activator, the animation would act upon #activator, QED $(this).

María Pérez de Arrilucea
María Pérez de Arrilucea
6,490 Points

Thanks a lot Matthew! I understand it perfectly now :)