Skip to content Skip to sidebar Skip to footer

Generalizing Jquery Dynamic Add/remove Form Input For Multiple Forms

Currently I have a functioning code for dynamically adding and removing form inputs on my page. I have multiple forms that I need to include on the page, so I made an event action

Solution 1:

Okay, I solved it. There were multiple issues with my selectors that I had to fix, but after that the following solution works perfectly:

$("#cc_button").click(function(){
  $("#table1").show();
  $("#table2").hide();
  $("#cctable tr").attr('class', 'dinput');
  $("#dbtable tr").attr('class', 'dbinput');
});
$("#db_button").click(function(){
  $("#table2").show();
  $("#table1").hide();
  $("#dbtable tr").attr('class', 'dinput');
  $("#cctable tr").attr('class', 'ccinput');
});

This basically changes the class attribute of each table according to which button is pressed. Theoretically, this should work for 4 forms, though I have not tried it yet. This is the updated code (I've changed a lot since the first code) for those that want to see what I did:

<html><head><title></title><scriptsrc="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script><scriptsrc="jquery.maskedinput.js"type="text/javascript"></script><scripttype="text/javascript">
    $(document).ready(function() {
        $("#table1").hide();
        $("#table2").hide();
        $("#cc_button").click(function(){
            $("#table1").show();
            $("#table2").hide();
            $("#cctable tr").attr('class', 'dinput');
            $("#dbtable tr").attr('class', 'dbinput');
        });
        $("#db_button").click(function(){
            $("#table2").show();
            $("#table1").hide();
            $("#dbtable tr").attr('class', 'dinput');
            $("#cctable tr").attr('class', 'ccinput');
        });
        $('.btnAdd').click(function() {
            var num     = $('.dinput').length; // how many "duplicatable" input fields we currently havevar newNum  = newNumber(num + 1);      // the numeric ID of the new input field being added// create the new element via clone(), and manipulate it's ID using newNum valuevar newElem = $('.dinput:last').clone();

            // insert the new element after the last "duplicatable" input field
            $('.dinput:last').after(newElem);
            $(".dinput:last td:first").replaceWith( "<td>" + newNum + "</td>" );

            // enable the "remove" button
            $('.btnDel').attr('disabled','');

            $(".date").mask("99/99/9999");

            // business rule: you can only add 20 namesif (newNum == 20)
                $('.btnAdd').attr('disabled','disabled');
        });

        $('.btnDel').click(function() {
            var num = $('.dinput').length; // how many "duplicatable" input fields we currently have
            $('.dinput:last').remove();     // remove the last element// enable the "add" button
            $('.btnAdd').attr('disabled','');

            // if only one element remains, disable the "remove" buttonif (num-1 == 1)
                $('.btnDel').attr('disabled','disabled');
        });

        $(".date").mask("99/99/9999");
    });
</script></head><body><divid="tablebuttons"><buttontype="button"id="cc_button">CC</button><buttontype="button"id="db_button">DB</button></div><divid="table1"><formid="ccards"method="post"action="<?phpecho htmlentities($PHP_SELF); ?>"><tableborder="1"cellspacing="0"><tr><th></th><th># (last four digits)</th><th>Amount</th><th>Approval</th><th>Date Received</th></tr><tbodyid ="cctable" ><trclass="ccinput"><td> 1 </td><td><inputtype="text"name="cc_num[]"maxlength="4" /></td><td><inputtype="int"name="cc_amnt[]" /></td><td><inputtype="text"name="cc_app[]"maxlength="10" /></td><td><inputclass="date"type="text"name="cc_date[]" /></td></tr></tbody></table><div><inputtype="button"class="btnAdd"value="Add" /><inputtype="button"class="btnDel"value="Remove" /></div><inputtype="submit"value="Submit"name="submit" /></form></div><divid="table2"><formid="db"method="post"action="<?phpecho htmlentities($PHP_SELF); ?>"><tableborder="1"cellspacing="0"><tr><th></th><th>DB #</th><th>Amount</th><th>Date</th></tr><tbodyid="dbtable"><trclass="dbinput"><td> 1 </td><td><inputtype="text"name="db_num[]" /></td><td><inputtype="int"name="db_amnt[]" /></td><td><inputclass="date"type="text"name="db_date[]" /></td></tr></tbody></table><div><inputtype="button"class="btnAdd"value="Add" /><inputtype="button"class="btnDel"value="Remove" /></div><inputtype="submit"value="Submit"name="submit" /></form></div></body></html>

Solution 2:

You can use something like this.

//when the Add Field button is clicked
$("#add").click(function (e) {
//Append a new row of code to the "#items" div
  $("#items").append('<div><input type="text" name="input[]"><button  class="delete">Delete</button></div>');
});

for detailed tutorial http://voidtricks.com/jquery-add-remove-input-fields/

Post a Comment for "Generalizing Jquery Dynamic Add/remove Form Input For Multiple Forms"