Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.79 KB | None | 0 0
  1. function add_field(addToo,type)
  2. {
  3.     switch(type)
  4.     {
  5.         case 'email':
  6.             add_email(addToo);
  7.             break;
  8.     }
  9.     // More case statements will be added later
  10. }
  11.  
  12. function add_email(addToo)
  13. {
  14.     ++numEmail;
  15.     var container = document.getElementById(addToo);
  16.     // New parent DIV
  17.     var div=document.createElement('div');
  18.     div.id='email_'+numEmail;
  19.     // New input text box
  20.     var txt=document.createElement('input');
  21.     txt.type='text';
  22.     txt.name="email["+numEmail+"]";
  23.     // Select box
  24.     var sel=document.createElement('select');
  25.     var opt1=document.createElement('option');
  26.     var opt2=document.createElement('option');
  27.     var opt3=document.createElement('option');
  28.     sel.name='email_type['+numEmail+']';
  29.     opt1.appendChild(document.createTextNode('Home'));
  30.     opt2.appendChild(document.createTextNode('Work'));
  31.     opt3.appendChild(document.createTextNode('Parent(s)'));
  32.     opt1.value='Home';
  33.     opt2.value='Work';
  34.     opt3.value='Parents';
  35.     sel.appendChild(opt1);
  36.     sel.appendChild(opt2);
  37.     sel.appendChild(opt3);
  38.     // Remove link
  39.     var a=document.createElement('a');
  40.     a.setAttribute('href','#');
  41.     a.setAttribute('onclick', "remove_node('email_"+numEmail+"'); return false;");
  42.     a.appendChild(document.createTextNode('remove'));
  43.  
  44.     a.style.marginLeft='1.1em';
  45.  
  46.     div.appendChild(txt);
  47.     div.appendChild(sel);
  48.     div.appendChild(a);
  49.     container.appendChild(div);
  50. }
  51.  
  52.  
  53. /*
  54.  * Remove a child node from an HTML element.
  55.  * id: the id of the node you wish removed from DOM.
  56.  */
  57. function remove_node(id)
  58. {
  59.     var elem=document.getElementById(id);
  60.     elem.parentNode.removeChild(elem);
  61. }
  62.  
  63.  
  64.  
  65. <p><a href="" onclick="add_field('emails','email');return false;">Add Email</a></p>
  66. <div id="emails"></div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement