Mathias Rechtzigel
Mathias Rechtzigel

Reputation: 3604

Grabbing all ID's within a form and putting them in an array ( input, select & textarea )

Here's the fiddle I'm using that shows what currently works.

I'm running a loop that checks all of the ID's within an array and whether or not they have a valid input (in this case if the length of the input is greater than 0). I capture all of those ID's by running this snippet:

$('#submitButton').click(function(){
    // Gets the ID's and put it in an array
    contactID = $('#contact-form input[id]').map(function() {
      return this.id;
}).get();

Now I need to grab the ID's of select and textarea in addition to the input.

I tried changing it to the following which I would assume would grab the inputID AND selectID AND textareaID:

contactID = $('#contact-form input[id]' && '#contact-form select[id]' && '#contact-form textarea[id]').map(function() {
      return this.id;
    }).get();

What would be the most succinct way of grabbing all of these ID's and putting them into the contactID array?

Upvotes: 0

Views: 47

Answers (1)

stephband
stephband

Reputation: 2643

&& is a logical operator. The result of:

'string' && 'string'

is

true

so effectively your jQuery object is

$(true)

What you really want as a jQuery selector could be this:

$('#contact-form [id]')

or if you really need to be specific, this:

$('#contact-form input[id], #contact-form textarea[id], #contact-form select[id]')

But that is not the only problem you have in your code. You are missing some closing brackets for the map function.

Upvotes: 1

Related Questions