Accessing an array of HTML input text boxes using jQuery or plain Javascript -


I am looking for a form that has a dynamic number of input text boxes. I would like to add each text box as part of an array (it simplifies the loop for me in theory, especially when I do not know the number of text fields which will eventually exist). HTML code would like something like this:

  & lt; P & gt; Field 1: & lt; Input type = "text" name = "field [1]" id = "field [1]" & gt; & Lt; / P & gt; & Lt; P & gt; Field 2: & lt; Input type = "text" name = "field [2]" id = "field [2]" & gt; & Lt; / P & gt; & Lt; P & gt; Field 3: & lt; Input type = "text" name = "field [3]" id = "field [3]" & gt; & Lt; / P & gt; & Lt; P & gt; Field 4: & lt; Input type = "text" name = "field [4]" id = "field [4]" & gt; & Lt; / P & gt; & Lt; P & gt; Field 5: & lt; Input type = "text" name = "field [5]" id = "field [5]" & gt; & Lt; / P & gt;  

This data will then be sent to PHP scripts and presented as an array - or at least, this is the principle.

So my first question is, is it possible to use HTML? Are you designed to work in this way?

If the answer is "yes", then how would I like to use them all about jQuery or is that, plain old javascript?

I have tried to get it using the following jQuery code:

  someval = $ ('# field [1]'). Val ();  

and

  some = $ ('# area') [1] .val ();  

and the following javascript:

  someval = document.getElementById ('related_link_url') [1]. value;  

But I had no luck.

Thanks in advance.

Edit:

I should note that from Javascript Point, I have done this, where the id field_1, field_2 etc. of each element is. However, I think that if I can get it by placing each text box in an array, it makes the code harder and easier to manage.

First of all, enter the id attribute in [ Or ] characters can not be included.

There are several ways to get jquery / plain javascript references for these elements. You can use the line selector:

  & lt; Fieldet id = "list-of-field" & gt; & Lt ;! - Here's your information - & gt; & Lt; / Fieldset & gt; $ ("# Field-of-field input"); Document.getElementById ("list ....") getElementsByTagName ("Input").  

You can also use the attribute selector:

  $ ("input [name = = field]");  

I'm not sure that this is the only way, but I think in plain JavaScript, you have to bring all the input element ( document.getElementsByTagName) . ) and then check the loop through the array of these elements and each element (regardless of the name attribute, starting with the value field ) .


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -