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

HTML Build an Interactive Website Form Validation and Manipulation Checking Values

jQuery Challenge Help

Create a method called 'requiredValues' that returns an array of all the values of inputs with the class of 'required'.

function isValidEmail(email) {
  return email.indexOf("@") != -1;
}
function requiredValues() {
  var required = new Array();
  return.Array(.required);
}

2 Answers

Jacob Miller
Jacob Miller
12,466 Points

Here's the answer:

var requiredValues = function() {
    var myArray = new Array();
    $(".required").each(function() {
        myArray.push($(this).val());
    });
    return myArray;
}

What this is doing is first creating a new array, then selecting all inputs with the class of .required, then it iterates through each one with the .each() function, gets the value of each input with $(this).val(), and adds the value of that specific input onto the the array myArray with the .push() function. Finally it returns the array myArray.

Jacob Thanks for the explanation it helped to understand the code