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
Matthew Atkins
22,916 PointsTroubles with "checking values" jQuery code challenge in "Form Validation and Manipulation" lesson.
Up until now I haven't really had any problems, but this challenge just isn't agreeing with me.
In task 2/3, the question is "Create a method called 'requiredValues' that returns an array of all the values of inputs with the class of 'required'."
I've tried writing this about every way I can think of after watching the video, but I just can't get past it. Would anyone be able to help out with how this should be writtin?
The code looks like this at this point.
function isValidEmail(email){
return email.indexOf("@") != -1;
}
5 Answers
J.T. Gralka
20,126 PointsHey Matthew,
Remember that the question is asking you to "Create a method called 'requiredValues' that returns an array of all the values of inputs with the class of 'required'."
The first step in the right direction would be to create a local variable to that method that instantiates an array. We'll use the array to store the values of the required fields using jQuery.
function requiredValues(){
// This is a new array; we'll store the values in 'required'
var values = new Array();
// Now, we'll use jQuery to map the values of the input fields with the class 'required'
}
Remember that in the video, Andrew demonstrates how to use the $.each() method to start adding values from the required fields. Try rewatching the video and seeing if you can remember how to do this.
If you need a hint, remember that you can use an anonymous function as the argument to the each method. Also make sure that you're using the right selector for this method. Here's an example of how I might select all of the elements with a class named myClass
$(".myClass").each(function(){
// What do I want to do?
});
Let me know if that helps clear things up.
Best,
J.T.
Matthew Atkins
22,916 PointsThanks J.T.
Between your answers and the video, I have something that looks like the following:
function requiredValues(){
var values = new Array();
$(".required").each(function(){
values.push($(this).val() == "");
});
return values.sort().pop(); }
Which is still not validating with the message "There is something wrong with your code in your 'requiredValues' function."
Seems to me like this should work. But I suppose something isnt quite right.
Thanks again.
Andrew Chalkley
Treehouse Guest TeacherYou're almost there.
The requiredValues is only asking for the values. The .val() method returns the value of a given field. This particular step isn't asking you to compare it to the empty string. So all you need to do is remove == "" and just return values;.
However the next step in the challenge will be asking you to do what you're doing there in a containsBlanks() method. :)
Regards Andrew
Matthew Atkins
22,916 PointsAnd so my end result looks like :
function requiredValues(){
var values = new Array();
$(".required").each(function(){
values.push($(this).val());
});
return values; }
and validates.
Thanks much for your help guys.
Andrew Chalkley
Treehouse Guest TeacherWell done!