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
Amanda Swartz
2,098 PointsPlease help
Create a method called 'requiredValues' that returns an array of all the values of inputs with the class of 'required'.
var $required = $(".required");
function requiredValues(){
var blanks = new Array();
$required.each(function(){
blanks.push($(this).val() == "");
});
};
I've watched and rewatched the video for the last several hours. I've copied Andrew's code directly. I've tried every sensible and nonsense variation of codes and bracket arrangements and I can't get this to work. Please tell me what to write so that I can move on.
6 Answers
James Barnett
39,199 PointsAmanda - I haven't done the JQuery course yet, so I won't but much help. However, here's a suggestion from, your friendly neighborhood mod ...
You might get better responses to your query if, add a more descriptive title. You might be interested in checking out this guide on how to ask good questions on the forum.
Andrew Chalkley
Treehouse Guest TeacherThe question asks for the values. You only need $(this).val() to be pushed on to the array. You don't need == "" as that will compare it to the empty string and return a true or false not the value.
Remember what you've done here as the next step will check for blanks which is what you'll need the == "" for.
Amanda Swartz
2,098 PointsThank you. I think I still don't get it.
var $required = $(".required");
function requiredValues(){
var blanks = new Array();
$required.each(function(){
blanks.push($(this).val());
});
};
This is what I have now. Not working. There is something wrong with my code apparently. Soon there is going to be something wrong with my mind and I'm going to get a padded room.
Andrew Chalkley
Treehouse Guest TeacherSorry one last thing, on the last line of the requiredValues function return blanks;
The question asks for you to return an array of the values.
Create a method called 'requiredValues' that __returns an array_ of all the values of inputs with the class of 'required'._
On a slight side note, the array in your function is called blanks. Although the function will work the array doesn't describe what you're storing so it could get confusing for someone else coming to edit your code. Maybe rename the array to values :)
Amanda Swartz
2,098 PointsThanks, I Googled it and found where you had answered this in the past.
samiff
31,206 PointsHey Amanda, I've answered this in the past here, might make it easier to understand the process:
After you've declared the function, you have to create a new array, then using a jQuery selector, you target all inputs with the "required" class name. Each of those elements are now part of a jQuery object, and we can use the each method to iterate over every element and execute a function on it.
What you want to do inside that function is add the value of the current element to the created array. An easy way to do that is by pushing the value onto the array. You can access the value by calling .val() on the current element, which is easily referenced using "$this". Don't forget to return your array.
var requiredValues = function()
{
var myArray = new Array();
$('.required').each(function()
{
myArray.push($(this).val());
});
return myArray;
}