|
Using JQuery to dynamically add form fields to a page |
I'd seen a few different ways of dynamically adding form fields to a web page but hadn't ever tried it. I was interested in seeing how exactly you can do this at a technical level, but also how you can manage the users interactions, allowing them to keep adding fields as they complete the previously added ones.
Firstly lets define a few goals:
1. I want to add new form fields to an existing form, and they must have unique ids / names.
2. I want to make sure that a user can keep adding more new fields but only after they have completed the previous added field.
Let's setup the form first.
2 <label for="static">Static form field</label>
3</div>
4<div class="form-field">
5 <input type="text" name="static" id="static" size="20">
6</div>
7
8<div id="newfields"></div>
9
10<div id="more-link" class="clear-both big"></div>
11
12<div class="form-label"></div>
13<div class="form-field">
14 <input type="submit" name="Action" value="submit">
15</div>
The first form field is a 'normal' static field, nothing exciting going on here. Next there is a div element ''newfields" this is a container for our newly added fields.
Next we have a div "more-link" this contains a JQuery powered link that users will click on to add new fields. I'm creating this in JavaScript so that I can turn the link on and off based on some conditional logic.
Now lets take a look at the JQuery that will populate these divs.
2// create an index value
3var index = 1;
4
5// adds a new field
6function addfield() {
7 index++;
8 var appendStr = '<div class="form-label"><label for="newfield'+index+'">New field no '+index+'</label></div>'
9 appendStr += '<div class="form-field"><input id="newfield'+index+'" name="newfield'+index+'" size="20" class="exit-detect"></div>'
10
11 $('#newfields').append(appendStr);
12 // turn off the click event to create new fields
13 removeStr();
14 }
15
16 // disables the hyperlink
17 function removeStr(){
18 $('#more-link').html('Add field')
19 }
20
21 // enables the hyperlink
22 function addStr(){
23 $('#more-link').html('<a href=javascript:void(0); id=add-field>Add field</a>')
24 }
25
26 $(document).ready(function(){
27
28 // create the link when the dom loads
29 addStr();
30
31 // use live() to create a click event
32 $('#add-field').live('click', function() {
33 addfield();
34 });
35
36 // did the user click out of the new field, does it have a value?
37 $('.exit-detect').live('blur', function() {
38
39 // check if the field length is gt 0
40 if($(this).val().length > 0){
41 // turn the link back on
42 addStr();
43 }
44
45 });
46
47 });
48</script>
There is a fair bit of code here, so we'll break it down. Initially I create an index value. This will be incremented each time we create a new field.
Next we have a function 'addfield', this creates the form elements and appends them into the div 'newfields'. It then disables the hyperlink that creates the fields.
Next there are two functions 'removeStr' and 'addStr' these create or destroy a hyperlink to allow users to make new fields. They are called from other functions here.
Now we are inside our document.ready, so this is going to fire when the DOM has loaded. Firstly we create the link to add fields using 'addStr()'.
Next there is a click event on the selector '#add-field'. This runs the function (addfield) that creates the form fields.
Lastly there is a selector on the element 'exit-detect' that runs a blur event on the form field that was created. It checks that the newly created field has a value in it, if it does then it re-enables the hyperlink, allowing the user to create more fields. In this way a user cannot just click 'add' a dozen times and create a load of blank fields.
Note that most of these selectors are using the live() method to bind an event as the dom elements don't exist on page load.
Follow this link to view a demo of using JQuery to dynamically add form fields.