Jump to content

Finding all form inputs whose name/id begins with...


Recommended Posts

I'm creating some form inputs dynamically with php and naming them in a box_n fashion (n being an incrementing int). Any ideas on how I would go about fetching the contents of all of these inputs with JavaScript? Do I somehow loop through all form objects and check a substring-ed name/id for a match, or is there a simpler way?

Many thanks in advance.
It wouldn't let me post with the script tags. This will fetch the names.

[code]
<!-- javascript -->
function find_boxes() {
var boxes = new Array();
var inputs = document.getElementsByTagName('input');
for (var i = 0; i < inputs.length; i++) {
var name = inputs[i].name;
if (name.match(/^box_\d+$/)) {
boxes.push(name);
}
}
alert(boxes);
}
<!-- /javascript -->
<form name="test_form">
<input name="box_1"/>
<input name="box_2"/>
<input name="box_3"/>

<input name="name"/>
<input name="address"/>
<input name="etc"/>

<input name="box_4"/>
<input name="box_5"/>
<input name="box_6"/>
</form>
<input type="button" onclick="javascript:find_boxes()" value="Find the boxes"/>
[/code]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.