Posted By

jackkeller on 11/01/11


Tagged

jquery


Versions (?)

jQuery Form from Array


 / Published in: JavaScript
 

  1. $(document).ready(function() {
  2. $('#forms').css({'border': '1px solid #333333', 'padding': '10px'});
  3. var forms = ["tname", "temail", "tphone", "sinquiry", "amessage"],
  4. index;
  5. var form = $('#forms');
  6. for (index = 0; index < forms.length; index++) {
  7. var indextype = forms[index].substring(0,1);
  8. var indexname = forms[index].substring(1);
  9.  
  10. form.append('&lt;label for="'+indexname+'"&gt;'+indexname+'&lt;/label&gt;\n');
  11. switch(indextype) {
  12. case "t":
  13. form.append('&lt;input type="text" name="'+indexname+'" id="'+indexname+'" /&gt;\n\n');
  14. break;
  15. case "s":
  16. form.append('&lt;select name="'+indexname+'" id="'+indexname+'"&gt;\n\t&lt;option&gt;&lt;/option&gt;\n\t&lt;option&gt;&lt;/option&gt;\n&lt;/select&gt;\n\n');
  17. break;
  18. case "a":
  19. form.append('&lt;textarea name="'+indexname+'" id="'+indexname+'"&gt;&lt;/textarea&gt;\n\n');
  20. break;
  21. }
  22. }
  23. var output = $('#forms').html();
  24. output = output.replace(/&lt;/g,'<',output);
  25. output = output.replace(/&gt;/g,'>',output);
  26. output = output.replace(/\n/g,'<br />',output);
  27. output = output.replace(/\t/g,' ',output);
  28. $('#theform').html(output);
  29. });
  30.  

Report this snippet  

You need to login to post a comment.