greyhat49

js jquery

Dec 6th, 2014
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.52 KB | None | 0 0
  1. <html>
  2. <head>
  3. </head>
  4. <body>
  5. <form id="myForm">
  6.     <div id='TextBoxesGroup'>
  7.         <div id="nmKorban1">
  8.             <input type="text" class="col-xs-12 col-sm-6"  name="nmKorban1" placeholder="Nama Korban"></input>
  9.         </div>
  10.     </div>
  11.     <input type='button' value='Add Button' id='addButton'>
  12.     <input type='button' value='Remove Button' id='removeButton'>
  13.     <a href='' id='sub'>asda</a>
  14. </form>
  15.  
  16.  
  17. <script src="jquery.js"></script>
  18. <script type="text/javascript">
  19.             $(document).ready(function(){
  20.              
  21.                 var counter = 2;
  22.                 var a = $(this).attr('name');
  23.                
  24.                 $("#addButton").click(function () {
  25.                
  26.                 if(counter>10){
  27.                         alert("Only 10 textboxes allow");
  28.                         return false;
  29.                 }  
  30.              
  31.                 var newTextBoxDiv = $(document.createElement('div'))
  32.                      .attr("id", 'nmKorban' + counter);
  33.              
  34.                 newTextBoxDiv.after().html('<br><br>'+
  35.                       '<input type="text" class="col-sm-6" name="nmKorban' + counter +
  36.                       '" id="nmKorban' + counter + '" value="" placeholder="Nama Korban">');
  37.              
  38.                 newTextBoxDiv.appendTo("#TextBoxesGroup");
  39.              
  40.              
  41.                 counter++;             
  42.                  });
  43.              
  44.                  $("#removeButton").click(function () {
  45.                 if(counter==2){
  46.                       alert("No more textbox to remove");
  47.                       return false;
  48.                    }  
  49.              
  50.                 counter--;
  51.              
  52.                     $("#nmKorban" + counter).remove();
  53.              
  54.                  });
  55.                 $('#sub').click(function(e){
  56.                     e.preventDefault();
  57.                     var data = $('#myForm').serialize();
  58.                     //var i = $(this).attr('id');
  59.                     console.log(data);
  60.                 });
  61.               });
  62.         </script>
  63. </body>
  64. </html>
Advertisement
Add Comment
Please, Sign In to add comment