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

JavaScript

Stuck on question!

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

1 Answer

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Shaun;

Let's break this task down...

Instructions

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

Steps

Create a method called requiredValues

var requiredValues = function() {}

... that returns an array

var requiredValues = funtion() {

    return taskArray;
}

... of all the values of inputs with the class of required.

var requiredValues = function()
  {
     var taskArray = new Array(); // create a new array
           // use a jQuery selector to target inputs with the "required" class
           // use the "each" method to iterate over every element
     $('.required').each(function()  
     {
           taskArray.push($(this).val()); // add all of the values to the array
     });
     return taskArray;
  }

Hope it helps and happy coding,

Ken