It was not too precisely indicated what to call this question, but here is the problem I am having:
I need to be able to submit an array or linked list of data via an HTML form. Since this array can have a variable length, I have two options:
- Use some form of delimiter (for example, comma-delimited items in a list)
- Import multiple inputs (for an array element) dynamically generated when new elements are added.
I would like to continue the second option, although I felt that since there will be many such “arrays” in this particular project, I have to create a jQuery plugin so that I can more easily implement this function in future forms.
As I work on this, I also added functionality for several types of input, selected, selected multipliers (through a separator) and related inputs (for example, with two text inputs for “first name” and “last name” which should be presented as a pair)
I have a basic layout, as I think it should work:
(function($) {
This code is called on the inputs that you want to replace with a dynamic list. If you select multiple inputs, they will become a list of related data (for example, an example of the first and last name). An example of how to call this will be as follows:
<html> <head> <script> $('#fn,#ln').dynamicList(); </script> </head> <body> <form> <input id="fn" name="first_name" /> <input id="ln" name="last_name" /> </form> </body> </html>
This will create a dynamic list where you can enter the first and last name, and then click the plus button (now on the right or under the name of the last name based on your CSS rules), and it will add a row to the table containing the first name in one column, the last name in the other and the minus button in the last. You can continue to enter names and press this plus button, and the table expands unlimitedly down. When submitting a form (using the submit button or any other method), the table cells must become inputs.
I thought the first problem I encountered was in the plus handler. Since I'm inside the function called by clicking the plus button, the keyword this now refers to the DOM plus element of the icon. I cannot capture the value of the inputs to add them to the table. One of the proposed solutions was to create a global variable, which is a copy of an object for which the dynamic list function was called like this:
. . . plus.after( table ); var dynamicList = this; this.delegate( plus, 'click', function() { for( i = 0; i < dynamicList.length; i++ ) {
My concern is that if I create multiple dynamic lists, they will override each other. It's true? If so, how can I overcome this?
The second problem I ran into was similar. After submitting the form, I need to find the table to get the values to submit AND find the inputs to get the names under which they should be submitted.
However, going around the code to try to fix it, I ran into a problem much earlier than expected.
Uncaught TypeError: Object [object Object] has no method 'replace' jquery-1.6.2.min.js:16 f.each.f.fn.(anonymous function) jquery-1.6.2.min.js:17 f.fn.extend.delegate jquery-1.6.2.min.js:17 $.fn.dynamicList jquery.dynamicList.js:76 (anonymous function) test.php:16 f.extend._Deferred.e.resolveWith jquery-1.6.2.min.js:16 e.extend.ready jquery-1.6.2.min.js:16 c.addEventListener.B jquery-1.6.2.min.js:16
Look at line 76 of my dynamic list, this is the following line:
this.delegate( plus, 'click', function() {
Can't I call .delegate() since "this" refers to multiple jQuery objects? Or can I not call .delegate() because "this" is not the parent of the plus sign? And as soon as this problem is solved, how to solve my other problems?
EDIT -
I changed the name and I found a solution to my problem on line 76. I am not sure exactly how .delegate() works, because I tried first:
this.first().parent().delegate( plus, 'click', function() {
and he was still upset. Then I realized that I could just use the .click() function (I kind of goofed)
plus.click( function() {
I used similar logic when creating the minus button for each row. I added minus.click() after creating the minus button and before inserting it into the DOM, eliminating the need to use .delegate() anywhere in the code.
I'm not sure how to get the values from the input fields inside the plus handler, since the keyword “this” was overwritten at this moment to refer to the plus button, and not to my input fields.