javascript - How to generate a input tag above a button -


i have input tag , link button. want click in button , generate input tag , other link button delete , click in delete button delete input tag. when click on button generate input tag under button. want generate input tag above button. how can that?

$(document).ready(function() {    var max_fields = 10; //maximum input boxes allowed    var wrapper = $(".input_fields_wrap"); //fields wrapper    var add_button = $(".add_field_button"); //add button id      var x = 1; //initlal text box count    $(add_button).click(function(e) { //on add input button click      e.preventdefault();      if (x < max_fields) { //max input box allowed        x++; //text box increment        $(wrapper).append('<div><input type="text" name="mytext[]"/><a href="#" class="remove_field">remove</a></div>'); //add input box      }    });      $(wrapper).on("click", ".remove_field", function(e) { //user click on remove text      e.preventdefault();      $(this).parent('div').remove();      x--;    })  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>  <div class="input_fields_wrap">    <button class="add_field_button">add more fields</button>    <div>      <input type="text" name="mytext[]">    </div>  </div>

you can use "prepend" instead of "append" accomplish this. check snippet.

$(document).ready(function () {          var max_fields = 10; //maximum input boxes allowed          var wrapper = $(".input_fields_wrap"); //fields wrapper          var add_button = $(".add_field_button"); //add button id            var x = 1; //initlal text box count             $(add_button).click(function (e) { //on add input button click              e.preventdefault();              if (x < max_fields) { //max input box allowed                  x++; //text box increment                  $(wrapper).prepend('<div><input type="text" name="mytext[]"/><a href="#" class="remove_field">remove</a></div>'); //add input box              }          });            $(wrapper).on("click", ".remove_field", function (e) { //user click on remove text              e.preventdefault();              $(this).parent('div').remove();              x--;          })      });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div class="input_fields_wrap">    <button class="add_field_button">add more fields</button>    <div><input type="text" name="mytext[]"></div>  </div>


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -