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 trialGuiomar Almunia
Courses Plus Student 7,062 PointsWhy it doesn´t work with var password instead of THIS??
I´ve made this exercice just changing the "this" reference to password to a
var password = $("#password").val();
Now the code doesn´t work. Do you now why? It is not basically the same??:
This WORKS:
function passwordEvent(){
$(this).val();
if($(this).val().length > 8){
$(this).next().hide();
}else{
$(this).next().show();
}
}
This DOESN´T WORK:
function passwordEvent(){
var password = $("#password").val();
if(password.length > 8){
$(password).next().hide();
}else{
$(password).next().show();
}
}
Thanks a lot and best regards!!
2 Answers
Steven Parker
231,275 PointsBaillie's mostly right (except that there are no variables declared inside the conditional statement). And while fixing up the jQuery, you could also simplify things a bit by removing the if...else and let the conditional directly control the display:
function passwordEvent() {
var password = $("#password").val();
$("#password").next().toggle(password.length <= 8);
}
But this is not the same as the original handler.
This version will only work with one input field that has the id "password". The original handler could be used with multiple input fields since "this" would always represent the specific input element that triggered the event.
Guiomar Almunia
Courses Plus Student 7,062 Pointsthanks a lot both of you. it helped a lot and it worked!! :)
Baillie O'Grady
9,883 PointsBaillie O'Grady
9,883 Pointsfunction passwordEvent(){
...You need to remove the parenthesis from the variables declared inside the conditional statement '$(password)' . so it looks like this '$password'. Also, add a dollar symbol before the variable so from 'var password = $("#password").val(); to 'var $password = $("#password").val();.
Hope this helps, i've typed asterisk against the code lines you need to change.