Click the box titles below to expand:

How to's

XSLT

GOOGLE MAPS API

PHP

Perl

Document Object Model (DOM)

UNIX

Generic Mapping Tools (GMT)

Miscellaneous

Projects

Courses Taught

Latest Favorites

Mac OS X

Web Development

Beta

How To :: Using the Document Object Model (DOM) for dynamic content

The DOM is a powerful force for dynamically changing webpage content based on user interaction without having to load/reload a webpage. This page is basically me documenting the experiments I have done using the DOM as applied to practical problems. I hope you find it useful.

Expand your form field dynamically

The problem: I was looking for a way to dynamically create form fields within a table so users can upload a photo caption and a file.

Solution 1: The Basics

My first solution worked on Safari, Mozilla (Apple & Windows) but not Internet Explorer for Windows. I was originally using the createElement DOM tag to create a new table row (<tr>) tag and two table cells (<td>). The names of the fields are slightly complex as I creating associative arrays for processing in PHP and I wanted to ensure the caption fields matched the file names. This solution is bascially a replication of Bin-Blog's solution.

expand_field.js

  1. <!--
  2. // Code to add a new form field
  3. function addField_1( area,captionName,captionID,fileName,fileID,limit ) {
  4.  
  5. if( !document.getElementById ) return ;
  6.  
  7. var field_area = document.getElementById(area) ;
  8. var all_inputs = field_area.getElementsByTagName("tr") ;
  9. var count = all_inputs.length - 1 ;
  10.  
  11. // If the maximum number of elements have been reached, exit the function.
  12. // If the given limit is lower than 0, infinite number of fields can be created.
  13.  
  14. if( count > limit && limit > 0 ) return ;
  15.  
  16. if( document.createElement ) { //W3C Dom method.
  17. var tr = document.createElement("tr") ;
  18. var td = document.createElement("td") ;
  19. var td2 = document.createElement("td") ;
  20.  
  21. var input = document.createElement("input") ;
  22.  
  23. input.id = captionID+count ;
  24. input.name = captionName+'['+count+']' ;
  25. input.size = 40 ;
  26. input.maxlength = 100 ;
  27. input.type = "text" ;
  28. td.appendChild(input) ;
  29.  
  30. var file = document.createElement("input") ;
  31.  
  32. file.id = fileID+count ;
  33. file.name = fileName+'['+count+']'+'[file]' ;
  34. file.type = "file" ;
  35.  
  36. td2.appendChild(file) ;
  37.  
  38. tr.appendChild(td) ;
  39. tr.appendChild(td2) ;
  40.  
  41. field_area.appendChild(tr) ;
  42. }
  43. }
  44. //-->
  45.  

Here it is in action:

Caption File

I still don't understand why it doesn't work in Windows IE (partially due to a poor DOM debugger for IE) but I had to get a solution quickly, so I started again.....

Solution 2: Back to the drawing board - I need it to work cross browser!

The solution I finally settled on works a little differently - it uses the DOM tags insertRow and insertCell. This method allows you create any number of table cells dynamically. I left the alert in so you can see which field number will be created. This solution is modified from the solution at mredkj.com

Here is a method to dynamically add form fields - a photo caption and a associated file:

expand_field_improved.js

  1. <!--
  2. // Improved DOM version to expand fields
  3. function addField_2( area,descriptionName,descriptionID,fileName,fileID,limit ) {
  4.  
  5. if( !document.getElementById ) return ; // Only DOM browsers
  6.  
  7. var field_area = document.getElementById( area ) ;
  8. var lastRow = field_area.rows.length ;
  9. var iteration = lastRow ;
  10. var row = field_area.insertRow( lastRow ) ;
  11.  
  12. // Cells
  13. var cellLeft = row.insertCell(0) ;
  14. var cellRight = row.insertCell(1) ;
  15.  
  16. var count = lastRow - 1 ;
  17.  
  18. // Comment out next line for production
  19. // alert('lastRow is '+lastRow+' iteration is '+iteration+' row is '+row+' count is '+count ) ;
  20.  
  21. // If the maximum number of elements have been reached, exit the function.
  22. // If the given limit is lower than 0, infinite number of fields can be created.
  23.  
  24. if( count > limit && limit > 0 ) return ;
  25.  
  26. if( document.createElement ) { // W3C Dom method.
  27.  
  28. var input = document.createElement('input') ;
  29. input.id = descriptionID+count ;
  30. input.name = descriptionName+'['+count+']' ;
  31. input.size = 40 ;
  32. input.maxlength = 100 ;
  33. input.type = 'text' ;
  34. cellLeft.appendChild( input ) ;
  35.  
  36. var file = document.createElement('input') ;
  37. file.id = fileID+count ;
  38. file.name = fileName+'['+count+']'+'[file]' ;
  39. file.type = 'file' ;
  40. cellRight.appendChild( file ) ;
  41. }
  42. }
  43. //-->
  44.  

See it action:

Caption File

I hope this exercise helps someone, although it is primarily to remind me how I did it and to link to the smarter people whose work I employed. Some useful references that I found on the web that pertain to this issue are listed below.

References

Comments

From: Jeff Gold, 2006-03-16

Rob-
 This page was extremely helpful. Thanks for sharing!

http://eqinfo.ucsd.edu/~rnewman/howtos/dom_action.html


One question, other then displaying an alert, do you know any other method
to help debug JavaScript?  I do most of my work in Java and Eclipse and
not having similar tools for Javascript just kills me.

Thanks again!
-Jeff
Hi Jeff,

Glad you found this resource useful. The best method for debugging Javascript 
that I employ is to run the web app from Firefox/Mozilla with the Javascript 
Console running. This does a DOM check and squeals if a variable is undefined, 
or you are trying to call a DOM-reference that does not exist.

Good luck!
- Rob

Disclaimer

This information is freely provided as–is. Messing around with the command line and creating files is a serious business, and I accept no liability for errors created, systems corrupted, or hard–disk damage by you following these instructions. They worked for me but may not work for you. Remember to back–up EVERYTHING before you try any of this stuff — it is not simple OR easy!!!

If you have any questions about this please email me at rlnewman@ucsd.edu and I will try my best to help you out.

made with CSS     Valid XHTML 1.0!      Valid CSS!