TMax
TMax

Reputation: 59

How to Generate a List of all Input name="" fields in a form with Javascript/JQuery

I'm sure there's a simple answer to this, but I'm a novice teaching myself Javascript and JQuery and I just don't know enough to figure it out myself. Any help you can provide is greatly appreciated.

I'm building a form that generates HTML emails for my company based on hundreds of different inputs entered by the form user. Rather than copying and pasting each and every input name into a $_POST line in the form's action script to retrieve the input's data, I'm wondering if there's a way to use Javascript/JQuery to generate a list of the name="" fields from each input on the form to make this easier?

For example, from the following slice of the code, how can I automatically generate a list that contains the name values "bottlespecial6image", "bottlespecial6imagewidth", "bottlespecial6imageheight", "bottlespecial6imagelink", and "bottlespecial6includeprice" (with the idea that my form has hundreds (if not thousands) of inputs, so copying/pasting seems inefficient):

<input type="text" name="bottlespecial6image" value=""/>
<input type="text" name="bottlespecial6imagewidth" value=""/>
<input type="text" name="bottlespecial6imageheight" value=""/>
<input type="text" name="bottlespecial6imagelink" value=""/>
<input type="radio" name="bottlespecial6includeprice" value="yes"  checked="checked" />
<input type="radio" name="bottlespecial6includeprice" value="no"  checked="checked" />

I apologize if this has already been covered here -- I searched around here for similar questions, but couldn't find anything.

Upvotes: 2

Views: 3702

Answers (2)

adeneo
adeneo

Reputation: 318202

To create a serialized array to submit, you'd use jQuery's serialize()

$('form').serialize();

to just get the names in an array, you can map them:

var inputs = $('form').find('input[name]'); //all inputs with a name in the form

var names  = $.map(inputs, function(el) { return el.name });

FIDDLE

Upvotes: 2

ManMohan Vyas
ManMohan Vyas

Reputation: 4062

$("[name='yourrequiredname']")

will give you list of all elements by same name

Upvotes: 0

Related Questions