HTML form input tag name element array with JavaScript
Solution 1
1.) First off, what is the correct terminology for an array created on the end of the name element of an input tag in a form?
"Oftimes Confusing PHPism"
As far as JavaScript is concerned a bunch of form controls with the same name are just a bunch of form controls with the same name, and form controls with names that include square brackets are just form controls with names that include square brackets.
The PHP naming convention for form controls with the same name is sometimes useful (when you have a number of groups of controls so you can do things like this:
<input name="name[1]">
<input name="email[1]">
<input name="sex[1]" type="radio" value="m">
<input name="sex[1]" type="radio" value="f">
<input name="name[2]">
<input name="email[2]">
<input name="sex[2]" type="radio" value="m">
<input name="sex[2]" type="radio" value="f">
) but does confuse some people. Some other languages have adopted the convention since this was originally written, but generally only as an optional feature. For example, via this module for JavaScript.
2.) How do I get the information from that array with JavaScript?
It is still just a matter of getting the property with the same name as the form control from elements
. The trick is that since the name of the form controls includes square brackets, you can't use dot notation and have to use square bracket notation just like any other JavaScript property name that includes special characters.
Since you have multiple elements with that name, it will be a collection rather then a single control, so you can loop over it with a standard for loop that makes use of its length property.
var myForm = document.forms.id_of_form;
var myControls = myForm.elements['p_id[]'];
for (var i = 0; i < myControls.length; i++) {
var aControl = myControls[i];
}
Solution 2
Try this something like this:
var p_ids = document.forms[0].elements["p_id[]"];
alert(p_ids.length);
for (var i = 0, len = p_ids.length; i < len; i++) {
alert(p_ids[i].value);
}
Solution 3
document.form.p_id.length
... not count().
You really should give your form an id
<form id="myform">
Then refer to it using:
var theForm = document.getElementById("myform");
Then refer to the elements like:
for(var i = 0; i < theForm.p_id.length; i++){
Solution 4
To answer your questions in order:
1) There is no specific name for this. It's simply multiple elements with the same name (and in this case type as well). Name isn't unique, which is why id was invented (it's supposed to be unique).
2)
function getElementsByTagAndName(tag, name) { //you could pass in the starting element which would make this faster var elem = document.getElementsByTagName(tag); var arr = new Array(); var i = 0; var iarr = 0; var att; for(; i < elem.length; i++) { att = elem[i].getAttribute("name"); if(att == name) { arr[iarr] = elem[i]; iarr++; } } return arr; }
Related videos on Youtube
ubiquibacon
ubiq·ui·ba·con - a side of a pig cured and smoked existing or being everywhere at the same time.
Updated on August 09, 2020Comments
-
ubiquibacon over 3 years
This is a two part question. Someone answered a similar question the other day (which also contained info about this type of array in PHP), but I cannot find it.
1.) First off, what is the correct terminology for an array created on the end of the name element of an input tag in a form?
<form> <input name="p_id[]" value="0"/> <input name="p_id[]" value="1"/> <input name="p_id[]" value="2"/> </form>
2.) How do I get the information from that array with JavaScript? Specifically, I am right now just wanting to count the elements of the array. Here is what I did but it isn't working.
function form_check(){ for(var i = 0; i < count(document.form.p_id[]); i++){ //Error on this line if (document.form.p_name[i].value == ''){ window.alert('Name Message'); document.form.p_name[i].focus(); break; } else{ if (document.form.p_price[i].value == ''){ window.alert('Price Message'); document.form.p_price[i].focus(); break; } else{ update_confirmation(); } } } }
-
Quentin almost 14 yearsWow. That's just ugly. Using
new Array()
instead of[]
, keeping a length tracker instead of usingarr.length
orarr.push()
, initializingi
outside the for loop, usinggetAttribute
instead of just going for the name property, working globally instead of within a form, and all the get (more or less) the same results ofdocument.forms.element.elements_name
. (Oh andgetElementsByName
could have been used too). -
ubiquibacon almost 14 yearsThanks, this is exactly what I was looking for. I modified it a little though...
for(i = 0; i < document.form.elements['p_id[]'].length; i++)
-
Quentin almost 14 yearsUsing
i
as a global isn't a good idea (it is just asking for scoping issues to bite you, either now or in the future because you have got into bad habits). Usingdocument.form
is bad practice because (a) thedocument.forms
collection will make it clearer what you are doing andform
isn't a very detailed name (anddocument.form
could be confused withdocument.forms
which makes maintenance more confusing. Scrunching things up on to a single line is fine though, I was just more explicit so you could see what is going on. -
Laoujin almost 11 years@Quentin:
for (var i...)
is in fact the same asvar i; for (i ...)
because JavaScript variables are function scoped, not block-scoped as is the case in many popular OO-languages. -
Quentin almost 11 years@Laoujin — typoknig didn't say
var i; for (i ...)
though. -
Michael Julyus Christopher M. over 4 yearsI have a problem when the form element is drop-down list. Can you give me solution for my question in stackoverflow.com/questions/58994834/…