Remove First Append if It Append Againe in Ajax

Occasionally we might find a demand to dynamically add and remove HTML elements like textbox into a spider web folio or application. Yeah occasionally, since information technology is very important that we accept all the required data with us before starting to design the forms or pages. This however does not rule out the possibility of such requirement. At that place have been instances where users wanted to have convenient access to adding every bit well as removing elements on their web pages.

Nosotros tin do this gracefully with a piffling help from jQuery methods. I am talking nearly methods like append(), after() and remove(), every bit these are the primary methods used for building a dynamic page with dynamically create elements. To be very honest, you don't have to exist a master of JavaScript to dynamically add or remove useful and interactive elements on your web page.

Dynamically Add together Textbox and Button using jQuery and Save the Data in an SQL Server Database

Dynamic-form-elements-using-jquery

See this demo

These elements can also have attributes and properties, all assigned dynamically while creating and adding them on the web page. And later on accomplishing their chore, the controls can be removed from the page. In fact, a single class or page tin can be designed to reuse again and again for various purpose.

How to Clone a Textbox Without its Value Using jQuery .clone() Method

The unabridged scenario tin can be understood with the help of a modest program. We will create a container in the grade of a DIV element, in which the dynamically created textboxes and push volition exist appended. So when the folio loads, it will display 3 predefined "buttons", whose primary function will be to add and remove the elements.

Along with the textboxes, we will also create and add a push to Submit the values in the textboxes. The Submit button will be created just once, and for the textboxes, we are setting a limit of 20 textbox per container.

How to add together contents dynamically to a DIV Chemical element using jQuery .appendTo() and .append() Method

The Markup and the Script

<html> <head>     <title>Create Elements Dynamically using jQuery</title>     <script src="https://ajax.googleapis.com/ajax/libs/jquery/ane.7.1/jquery.min.js"></script>     <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/one.nine.1/jquery-ui.min.js"></script> </head>  <body>     <p>Click the button and each will perform some dynamic function!</p>     <div id="primary">         <input type="button" id="btAdd" value="Add Element" course="bt" />         <input type="button" id="btRemove" value="Remove Element" class="bt" />         <input type="push" id="btRemoveAll" value="Remove All" grade="bt" /><br />     </div> </trunk>              <script>     $(document).fix(part() {          var iCnt = 0;                  var container = $(document.createElement('div')).css({             padding: '5px', margin: '20px', width: '170px', border: '1px dashed',             borderTopColor: '#999', borderBottomColor: '#999',             borderLeftColor: '#999', borderRightColor: '#999'         });          $('#btAdd').click(function() {             if (iCnt <= xix) {                  iCnt = iCnt + 1;                                   $(container).append('<input blazon=text class="input" id=tb' + iCnt + ' ' +                     'value="Text Element ' + iCnt + '" />');                                   if (iCnt == 1) {                     var divSubmit = $(document.createElement('div'));                     $(divSubmit).suspend('<input type=button class="bt"' +                          'onclick="GetTextValue()"' +                              'id=btSubmit value=Submit />');                 }                                   $('#main').after(container, divSubmit);             }                                       else {                       $(container).append('<label>Reached the limit</characterization>');                  $('#btAdd').attr('form', 'bt-disable');                  $('#btAdd').attr('disabled', 'disabled');             }         });                   $('#btRemove').click(function() {             if (iCnt != 0) { $('#tb' + iCnt).remove(); iCnt = iCnt - 1; }                      if (iCnt == 0) {                  $(container)                     .empty()            .remove();                   $('#btSubmit').remove();                  $('#btAdd')                     .removeAttr('disabled')                      .attr('course', 'bt');             }         });                   $('#btRemoveAll').click(function() {             $(container)                 .empty()                 .remove();               $('#btSubmit').remove();              iCnt = 0;                           $('#btAdd')                 .removeAttr('disabled')                  .attr('class', 'bt');         });     });           var divValue, values = '';      function            GetTextValue()            {         $(divValue)              .empty()              .remove();                   values = '';          $('.input').each(function() {             divValue = $(document.createElement('div')).css({                 padding:'5px', width:'200px'             });             values += this.value + '<br />'         });          $(divValue).append('<p><b>Your selected values</b></p>' + values);         $('body').append(divValue);     } </script> </html>

Attempt information technology

Dynamically Add Textbox and Button using jQuery and Save Data in SQL Server using Asp.Net Web Method and Ajax

An Overview of the Methods

Let us at present find out what methods we have used in the program and what are uses.

jQuery suspend() Method

The append() method will add elements ane later the other. A new element is added at the end of the last chemical element. This method is attached with the dynamically created DIV elements, which serves as a container.

$(container).append()

$(divSubmit).append()

Uncomplicated steps to Programmatically add LinkButton to a GridView Control – C# and Vb.Net

jQuery remove() Method

We used the remove() method for two detail reasons. The first purpose is to remove the controls one by one. The second time we used the method to remove all the elements attached to the container. Usually, the method remove() is used immediately after submitting the form or to reset a container or the entire folio.

jQuery after() Method

The after() method helps in keeping the sequence proper while adding all the elements to the main container. We volition add the submit push button subsequently all the textboxes have been added.

$('#main').subsequently(container, divSubmit);

Besides try… jQuery prepend() Method

This method will add an element at the kickoff of the list of elements. And then the newly added element will be at meridian of the list, followed by the elements added before. We are not using information technology in our demo, but you can practice it yourself by making a slight change.

Change this lawmaking…

$(container).append('<input blazon="text" id=tb' + iCnt + ' value=' + iCnt + ' />');

with this …

$(container).prepend('<input type="text" id=tb' + iCnt + ' value=' + iCnt + ' />');

Run into this demo

How to Create a Beautiful Dropdown Menu using Dynamic data extracted from an XML file using jQuery

Conclusion

Simple and yet useful, you can now add together any number of elements on your spider web page dynamically using the in a higher place procedures and methods. Even so, if you want to add text to an existing DIV element using jQuery, then I would recommend that you cheque this article at present.

Thank you for reading.

← Previous Side by side →

goosbyhishousy.blogspot.com

Source: https://www.encodedna.com/2013/07/dynamically-add-remove-textbox-control-using-jquery.htm

0 Response to "Remove First Append if It Append Againe in Ajax"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel