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
Adam Porter
12,362 PointsForm Validation and Manipulation, Checking Values, 2/3
Need some help figuring out the code for Checking Values question 2/3. The question is as follows...
Create a method called 'requiredValues' that returns an array of all the values of inputs with the class of 'required'.
10 Answers
Andrew Chalkley
Treehouse Guest TeacherHi Page,
It looks like total.push($(this).val(); is missing a closing bracket so it should look like this total.push($(this).val());
Hi Adam,
Does J.T.'s explanation help? Are you still having trouble?
Hi Richard,
Could you post your code? There maybe a missing bracket too?
Regards Andrew
Page Wood
Courses Plus Student 2,469 PointsI've been struggling with this question too.
I'm not understanding the portion where I add the items to the array. Here is what I have so far:
function isValidEmail(email) {
return email.indexOf("@") != -1;
}
function requiredValues () {
var required = $(".required");
var total = new Array ();
$required.each(function() {
total.push($(this).val();
})
return total;
}
J.T. Gralka
20,126 PointsHey Adam,
The key points to understand in answering this question are as follows:
You should be know how to...
- define a function in JavaScript & jQuery,
- create a local variable inside of that function and know how to instantiate an array as its value,
- use jQuery to select the content of fields whose class is "required" and add their values to the above mentioned array, and
- have the function return the complete array of elements.
What seems to be giving you trouble? Do you have any code that you've tried to run?
Regards,
J.T.
Richard Wilkerson
15,720 PointsI'm having a lot of trouble with this, too.
My "requiredValues" method looks like the one above from Page, down to every detail.
Does this differ greatly from the video lesson before, in that in the video we are only trying to see if a value is blank or not, and in this example, we are actually returning the values of a specific class?
Adam Porter
12,362 PointsHey Andrew,
JT's explanation helps some, along with the other comments on the page. I just can't seem to get the very last part of the question down, returning the array.
Any suggestions?
Andrew Chalkley
Treehouse Guest TeacherCan you show your code? You can use codepen.io or use markdown in a post.
Adam Porter
12,362 PointsHey I finally got it!!!
Here is what I did.
function isValidEmail(email){
return email.indexOf("@")!= -1;
}
function requiredValues () {
var $required = $(".required");
var $array = new Array ();
$required.each(function() {
$array.push($(this).val());
})
return $array;
}
Thank you for the help everyone.
Miriam Moser
4,261 PointsIt seems to be broken because it asks to select inputs with class required, but if you include inputs in the code, it won't pass. This is what I had, but it wouldn't go through until I deleted the word 'input'.
function requiredValues (){ var values = new Array(); $("input .required").each(function(){ values.push($(this).val()); }); return values; }
Richard Wilkerson
15,720 PointsThanks Andrew!
Here is my code so far (sorry, I don't yet know how to post code on here well):
function isValidEmail (email) {
return email.indexOf("@") != -1;
}
function requiredValues ( ) {
var required = $(".required");
var correct = new Array( );
$required.each(function( ){
correct.push($(this).val( ))
})
return correct;
}
I have the right brackets (I think) but it still does not work, not sure if I am selecting and arraying the elements right.
Richard Wilkerson
15,720 PointsAck - nevermind! I forgot the "$" symbols before my variables.
Andrew Chalkley
Treehouse Guest TeacherThere you go! :)
On a side, if you click on the markdown cheatsheet it tells you how to do code highlighting:
Code Indent your sentence with 4 spaces (or a single tab) to turn it into a code block.
I'll gone ahead and updated it for you :)
Antoine Guédés
884 PointsAntoine Guédés
884 PointsI got the same error you mentioned. I first written .push($(this)); Then written .push($(this)).val(); wich ofcourse is wrong.
Thanks a lot!